如何在Karate框架中动态更改Big JSON?

时间:2019-04-03 17:38:48

标签: json dsl karate

我有下一个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. 是否可以像这样在“更新”和“删除”部分中更改所有“ id”文件:

    • 设置req [*]。id = userId

1 个答案:

答案 0 :(得分: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}]
相关问题