2

I'm using Karate loops to generate dynamic Json. One of my test data contains array within an object. When I use karate loop on this data, the array is returned as an object.

* def fun = function(i){ return { "name": "userName"+ i, "email": "[email protected]", "id": "ID"+i, "testSheet": [{"sheetId" : "123"}]} }
* def jsonBody = karate.repeat(3, fun)
* print jsonBody

I'm expecting below:

[
  {
    "name": "userName0",
    "email": "[email protected]",
    "id": "ID1",
   "testSheet": [
        {
          "sheetId": "123"
        }
      ]
  }
]

But it returns this

[
  {
    "name": "userName0",
    "email": "[email protected]",
    "id": "ID1",
   "testSheet": {
       "0": {
          "sheetId": "123"
        }
      }
  }
]

Expected path: jsonBody[0].testSheet[0].sheetId

Actual path: jsonBody[0].testSheet.0.sheetId

1 Answer 1

2

Unfortunately this is a bug, which will be fixed in the next release: https://github.com/intuit/karate/issues/1187

This is the work-around. Define the array part outside the JS block for now, and use copy:

* def testSheet = [{"sheetId" : "123"}]
* def fun = function(i){ return { "name": "userName"+ i, "email": "[email protected]", "id": "ID"+i, "testSheet": testSheet } }
* copy jsonBody = karate.repeat(3, fun)
* print jsonBody
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.