-1

My script below:

    * def inputJson = createJson();
    * print inputJson

    * def customerName = karate.jsonPath(inputJson, "$.customerDetails[0]")
    * print customerName

Gives me this error:

javascript evaluation failed: karate.jsonPath(inputJson, "$.customerDetails[0]"), Property ['customerDetails'] not found in path $

Eventhough the jsonPath is valid as shown below:

Json path is working

Wondering if anyone here has encountered the same? Could you kindly advise me how to rectify this? Thank you in advance.

Note: I printed the json in the console and used the same when I checked the jsonPath in my screenshot above. I also validated in JSON Lint the json I was using and it is a valid one.

Drachir
  • 59
  • 7

1 Answers1

1

You don't need JsonPath in this case:

* def customerName = inputJson.customerDetails[0]

But if you have trouble with JsonPath, try karate.filter() as an alternative:

https://stackoverflow.com/a/62897131/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Yup, noticed that the simple one works (no need for jsonPath). My issue got resolved by modifying my java method. I have one more question though. Can the callonce be used with JS function? My JS returns json and I don't want to create json every time I run my scenarios. I only want to do the json creation once. Thank you! – Drachir Oct 09 '20 at 02:18
  • @Drachir yes it should work - note that you can even load the JSON in `karate-config.js` and search for something called `karate.callSingle()` in the docs – Peter Thomas Oct 09 '20 at 03:45