defining a variable to set length of an array is failing but assert and print works

1.7k views Asked by At
  • def count = * print response.teams[0].teamMembers.length throws below error

com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['length'] in path $['teams'][0]['teamMembers'] but found 'net.minidev.json.JSONArray'.
This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

  • print response.teams[0].teamMembers.length and
  • assert response.teams[0].teamMembers.length == 9
    are working just fine.

Any help here is much appreciated.

1

There are 1 answers

0
Peter Thomas On BEST ANSWER

Yes, Karate assumes the right-hand-side as Json-Path (which is fine for 90% of the cases). Use parentheses to force JavaScript evaluation when needed.

Try this:

def count = (response.teams[0].teamMembers.length)

For a detailed explanation, please refer to this section in the documentation: Karate Expressions