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

How to change big json dynamicly in Karate framework?

我有下一个JSON

{
  "updated": [
    {
      "id": "1",
      "email": "api.test@test.io",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 21,
      "gender": "male"
    },
    {
      "id": "2",
      "email": "api.test@test.io",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 22,
      "gender": "male"
    },
    {
      "id": "3",
      "email": "api.test@test.io",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 23,
      "gender": "male"
    }
  ],
  "deleted": [
    {
      "id": "4",
      "email": "api.test@test.io",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 31,
      "gender": "male"
    },
    {
      "id": "5",
      "email": "api.test@test.io",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 32,
      "gender": "male"
    },
    {
      "id": "6",
      "email": "api.test@test.io",
      "businessUnit": "US",
      "firstName": "John",
      "lastName": "Doe",
      "age": 33,
      "gender": "male"
    }
  ]
}
  1. 是否可以通过其他方式更改 'id' 文件而不是 :

    • 设置req.updated[0].id = userId
    • 设置req.updated[1].id = userId
    • 设置req.updated[2].id = userId
  2. 是否可以更改 'updated' 和 'deleted' 两个部分中的所有 'id' 文件,例如:

    • 设置请求[*].id = userId

EDITED:好的,所以你想使用批量编辑,并且有一些逻辑同时增加 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}]