Karate: Cannot use set to change values in a JSON array

Created on 2 Aug 2017  路  2Comments  路  Source: intuit/karate

Prior to a request being made in a Given, When, And block, I'm attempting to change an element in a JSON document whose form is roughly this:

[
    { "id": "Some value", "name": "A" },
    { "id": "Some other value", "name": "B" },
    { "id": "Yet another", "name": "C" }
]

I have two steps, one that builds the JSON document using some JavaScript and assigns the return value to a variable eventPayload. The second then attempts to modify the value of one of the "id" keys:

* def eventPayload = call read("classpath:generateJSONDoc.js")
* set eventPayload[1].id = "New value"

The problem is that in Script::setValueByPath() in Script.java, the JSON in eventPayload is rejected as it's type is neither JSON nor MAP (it's a JS_ARRAY).

Does set() only work on JSON objects? I've tried to use a JSONPath expression to specify the index too but that always returns a JS_ARRAY too.

I can't help feeling I'm missing something obvious here :)

bug fixed

Most helpful comment

Thanks @ptrthomas I'll give that a go, much appreciated and also a huge thanks for releasing Karate on github - it's a genuinely impressive and useful tool.

All 2 comments

@duncwebb apologies, this is indeed a bug (hopefully the last of the edge cases where js arrays were taking a different path through the code for set, now refactored, you can see the commit details).

Until the next release - just try type-conversion to json to see if this works:

* json eventPayload = call read("classpath:generateJSONDoc.js")
* set eventPayload[1].id = "New value"

Thanks @ptrthomas I'll give that a go, much appreciated and also a huge thanks for releasing Karate on github - it's a genuinely impressive and useful tool.

Was this page helpful?
0 / 5 - 0 ratings