You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Karate特性文件中从CSV生成复杂JSON请求的方法

在Karate中从CSV读取数据生成复杂JSON请求的简便实现方式

你需要生成的目标复杂JSON请求体如下:

[
  {
    "master": {
      "status": "string",
      // more fields
    },
    "address": [
      {
        "place": "string",
        // more fields
      }
    ],
    "product": [
      {
        "price": "string",
        // more fields
      }
    ],
    "payment": [
      {
        "action": "string",
        "currency": "string",
        // more fields
      }
    ],
    "referral": [
      {
        "reference": "string",
        // more fields
      }
    ],
    "customer_maintain": {
      "customer_master": {
        "gender": "string",
        // more fields
      },
      "customer_segment": [
        {
          "name": "string",
          // more fields
        }
      ]
    },
    "identification": [
      {
        "idname": "string",
        // more fields
      }
    ]
  }
]

以下是几种简便的实现方案:


1. JSON模板+CSV数据驱动(最常用)

这种方式将JSON结构与测试数据分离,维护性更强:

  • 第一步:创建JSON模板文件(比如request-template.json),用#{占位符}标记需要替换的动态字段:
[
  {
    "master": {
      "status": "#{status}",
      // 固定字段保留,动态字段用占位符
    },
    "address": [
      {
        "place": "#{address_place}",
        // 其他字段
      }
    ],
    "product": [
      {
        "price": "#{product_price}",
        // 其他字段
      }
    ],
    "payment": [
      {
        "action": "#{payment_action}",
        "currency": "#{payment_currency}",
        // 其他字段
      }
    ],
    "customer_maintain": {
      "customer_master": {
        "gender": "#{customer_gender}",
        // 其他字段
      },
      "customer_segment": [
        {
          "name": "#{segment_name}",
          // 其他字段
        }
      ]
    },
    "identification": [
      {
        "idname": "#{id_name}",
        // 其他字段
      }
    ]
  }
]
  • 第二步:准备CSV测试数据(比如test-data.csv),列名需与模板占位符完全匹配:
status,address_place,product_price,payment_action,payment_currency,customer_gender,segment_name,id_name
ACTIVE,Beijing,99.99,PAY,CNY,Male,VIP,ID12345
INACTIVE,Shanghai,49.99,REFUND,USD,Female,Regular,ID67890
  • 第三步:在Feature文件中实现数据驱动调用
Feature: 从CSV读取数据生成复杂请求调用API

  Scenario Outline: 批量调用API
    * def requestTemplate = read('classpath:request-template.json')
    * def request = karate.set(requestTemplate, __row)
    Given url 'https://your-api-endpoint.com/submit'
    And request request
    When method post
    Then status 200

    Examples:
      | read('classpath:test-data.csv') |

2. 直接在Feature中构造JSON+引用CSV数据

无需单独模板文件,直接在Feature里编写完整JSON结构,用__row.列名引用CSV数据:

Feature: 直接构造请求调用API

  Scenario Outline: 批量调用API
    * def request = 
    """
    [
      {
        "master": {
          "status": "#(__row.status)",
          // 其他字段
        },
        "address": [
          {
            "place": "#(__row.address_place)",
            // 其他字段
          }
        ],
        "product": [
          {
            "price": "#(__row.product_price)",
            // 其他字段
          }
        ],
        "payment": [
          {
            "action": "#(__row.payment_action)",
            "currency": "#(__row.payment_currency)",
            // 其他字段
          }
        ],
        "customer_maintain": {
          "customer_master": {
            "gender": "#(__row.customer_gender)",
            // 其他字段
          },
          "customer_segment": [
            {
              "name": "#(__row.segment_name)",
              // 其他字段
            }
          ]
        },
        "identification": [
          {
            "idname": "#(__row.id_name)",
            // 其他字段
          }
        ]
      }
    ]
    """
    Given url 'https://your-api-endpoint.com/submit'
    And request request
    When method post
    Then status 200

    Examples:
      | read('classpath:test-data.csv') |

3. 动态赋值复杂嵌套结构(适合字段极多的场景)

先初始化基础JSON结构,再用Karate的set语法逐个给嵌套字段赋值,灵活性拉满:

Feature: 动态赋值构造请求

  Scenario Outline: 批量调用API
    * def request = 
    """
    [
      {
        "master": {},
        "address": [{}],
        "product": [{}],
        "payment": [{}],
        "customer_maintain": {
          "customer_master": {},
          "customer_segment": [{}]
        },
        "identification": [{}]
      }
    ]
    """
    // 给嵌套字段逐一赋值
    * set request[0].master.status = __row.status
    * set request[0].address[0].place = __row.address_place
    * set request[0].product[0].price = __row.product_price
    * set request[0].payment[0].action = __row.payment_action
    * set request[0].payment[0].currency = __row.payment_currency
    * set request[0].customer_maintain.customer_master.gender = __row.customer_gender
    * set request[0].customer_maintain.customer_segment[0].name = __row.segment_name
    * set request[0].identification[0].idname = __row.id_name

    Given url 'https://your-api-endpoint.com/submit'
    And request request
    When method post
    Then status 200

    Examples:
      | read('classpath:test-data.csv') |

内容的提问来源于stack exchange,提问作者bulbasorus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:45:04