0

We are using restassured for API automation in our project . I have sample response for which I tested JsonPath expression on https://www.jsonquerytool.com/. My Json expression is - $[*]['tags'][?(@.id==0)]. I am getting proper output when I tried expression on JsonQuerytool. But when I try same in below code , I get invalid expression message -

JsonPath jsonPathEvaluator = response.jsonPath();

ArrayList result = jsonPathEvaluator.get("$[*]['tags'][?(@.id==0)]");

Above code throws exception .

Can anyone tell me how can I programmatically query the response using JsonPathEvaluator ?

P.S - Response not pasted as it was very huge.

Nilesh G
  • 103
  • 1
  • 9
  • What are you trying to query? If you don't provide any sample data and expectations from your query then at least describe it in free form text. – Alexey R. Aug 12 '22 at 11:25
  • Expression does not matter . As any expression that I am trying is throwing exception as invalid Json expression. Can Anyone tell me how to use Json Expression with JsonPath.get method. – Nilesh G Aug 12 '22 at 11:31

1 Answers1

0

Since your issue is not in any particular query (expression does not matter) I'm answering using example from the link you provided.

RestAssured uses some special JsonPath library (which uses its own syntax in some cases) called GPath. So if you have json like this:

{
    "key": "value",
    "array": [
        {
            "key": 1
        },
        {
            "key": 2,
            "dictionary": {
                "a": "Apple",
                "b": "Butterfly",
                "c": "Cat",
                "d": "Dog"
            }
        },
        {
            "key": 3
        }
    ]
}

And expect you would use query like this: $.array[?(@.key==2)].dictionary.a

Then for RestAssured case your query would be like this: array.findAll{i -> i.key == 2}.dictionary.a

So the complete code example would be:

public static void main(String[] args) {
    JsonPath jsonPath = RestAssured
            .get("http://demo1954881.mockable.io/gath")
            .jsonPath();
    List<String> resp = jsonPath.get("array.findAll{i -> i.key == 2}.dictionary.a");
    System.out.println(resp);
}
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • I changed $[*]['tags'][?(@.id==0)] to tags.findAll{tags->tags.id==0} . It is returning null . Is there any way I can use JsonPath expression in code and not Gpath. – Nilesh G Aug 12 '22 at 13:12
  • No. RestAssured does not let you change JsonPath processor. – Alexey R. Aug 12 '22 at 13:18
  • Can you tell me GPath equivalent for JsonPath $[*]['tags'][?(@.id==0)] ? – Nilesh G Aug 13 '22 at 05:27
  • I tried jsonPathEvaluator.get("tags.findAll{i -> i.id==0}"); also but it is returning NULL. Please tell me Gpath equivalent for JsonPath mentioned – Nilesh G Aug 13 '22 at 06:16
  • I am using below API - "https://petstore.swagger.io/v2/pet/findByStatus" . Parameter is '?status=available' – Nilesh G Aug 13 '22 at 06:34
  • Can anyone you please help me on this ? I am completely stuck due to this – Nilesh G Aug 13 '22 at 07:13
  • @NileshG I'll help if you give me your sample response and result you expect to fetch. – Alexey R. Aug 13 '22 at 07:19
  • I won't be able to paste response here as it is very huge but I have given you the URL - petstore.swagger.io/v2/pet/findByStatus and parameter is ?status=available. Once you get response , please tell me GPath equivalent for JsonPath $[*]['tags'][?(@.id==0)] . I want to get all tags with id=0. – Nilesh G Aug 13 '22 at 07:26
  • I see. Later today I'll take a closer look to your issue. Probably you will resolve it on your own by that time. There are some strange things. If your client accepts xml it returns one set of objects and if it accepts json it returns different set. So browser and rest assured get different content. And looks like there is no tags other than id=0. – Alexey R. Aug 13 '22 at 09:32
  • I have tried various combinations but it didn't work . This is showstopper for automation . I would appreciate if you can look into it today only. – Nilesh G Aug 13 '22 at 12:03
  • @NileshG `tags.findAll{i -> i.find{t -> t.id == 0}}` this works for me – Alexey R. Aug 13 '22 at 16:05
  • Many Thanks , it worked . Can you explain logic behind this . Also is there any online evaluator for Gpath , like the one we have for JasonPath . – Nilesh G Aug 14 '22 at 06:23