如何在Karate框架中动态更改大json?

问题描述 投票:1回答:1

我有下一个JSON

{
  "updated": [
    {
      "id": "1",
      "email": "[email protected]",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 21,
      "gender": "male"
    },
    {
      "id": "2",
      "email": "[email protected]",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 22,
      "gender": "male"
    },
    {
      "id": "3",
      "email": "[email protected]",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 23,
      "gender": "male"
    }
  ],
  "deleted": [
    {
      "id": "4",
      "email": "[email protected]",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 31,
      "gender": "male"
    },
    {
      "id": "5",
      "email": "[email protected]",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 32,
      "gender": "male"
    },
    {
      "id": "6",
      "email": "[email protected]",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 33,
      "gender": "male"
    }
  ]
}
  1. 是否可以通过其他方式更改“id”字段,而不是: set req.updated [0] .id =用户ID set req.updated [1] .id = userId set req.updated [2] .id = userId
  2. 是否可以更改“已更新”和“已删除”两个部分中的所有“ID”字段,如: set req [*]。id =用户ID
json dsl karate
1个回答
1
投票

编辑:好的,所以你想使用批量编辑,并有一些逻辑同时递增id-s。所以使用变换:https://github.com/intuit/karate#json-transforms

请注意,karate.map(x, i)采用可选的第二个参数,为您提供循环索引。

* def data = [{}, {}, {}]
* def fun = function(x, i){ x.id = ~~(i + 1); return x }
* def payload = karate.map(data, fun)
* match payload == [{id: 1}, {id: 2}, {id: 3}]
© www.soinside.com 2019 - 2024. All rights reserved.