underdoggo03
underdoggo03

Reputation: 23

Issue with creating a dynamic request body from external json file in karate

I am trying to create my request body dynamically from an external json file. I want to update few values and keep the remaining ones same as received from the json. The idea here is to keep one maintainable json file and manipulate it at run time to execute various scenarios.

Here's my feature file:

* def myJson = read('testFile.json')
* def requestBody = { "product": "#(myJson.product)", "properties": { "make": "#(brand)", "color": "#(myJson.color)" }
When request requestBody
And method post
Then status 200

Examples:
| brand |
| honda |

Contents of testFile.json are -

{
 "product": "car",
 "properties": {
 "make": "brand",
 "color": "red"
 }
}

The problem is that whenever there is nested json object, those fields won't keep the value from json. If the value is passed from the feature file as an example, then it gets evaluated correctly. Here's how the request body gets passed in the service call-

{
 "product": "car",
 "properties": {
 "make": "honda",
 "color": null
 }
}

I need the color key's value to be taken from myJson i.e. red but it get evaluated as null.

Upvotes: 2

Views: 590

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58153

Shouldn't it be:

"color": "#(myJson.properties.color)"

Upvotes: 1

Related Questions