0

Below is my response { "id": 123, "name": "text1" }, { "id": 456, "name": "text2" } ]

I want to find value of id whose name is 'text2'

Fenio
  • 3,528
  • 1
  • 13
  • 27
Rahul D
  • 13
  • 1

1 Answers1

0

Since Rest Assured uses Groovy's GPath you can use the following expression:

String json = "[{ \"id\": 123, \"name\": \"text1\" }, { \"id\": 456, \"name\": \"text2\" }]";
JsonPath path = JsonPath.from(json);
System.out.println(path.get("find { it.name == 'text2' }.id"));

The above code will return 456

It will work only if the JSON starts with an array []

Fenio
  • 3,528
  • 1
  • 13
  • 27