0

Here is JSON:

{
"first":0,
"rows":100,
"data":[
{
"id":326,
"tag":"QATA9",
"workNo":"qat12345"
}
],
"totalRecords":1
}

And my code is :

JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id");
System.out.println("id is "+ wID);

String responseBody = response.getBody().asString();
int statusCode = response.getStatusCode();

In output it shows [326] But i need value only 326

Vinay
  • 33
  • 1
  • 7

2 Answers2

3

The [] delimits an array, so the library is treating it as an array. Just pick the first element, and you should be fine.

Try this:

JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id")[0];
System.out.println("id is "+ wID);

Then, again, you should also have in mind that, the fact that an array was used in the first place may indicate that you may have more than one element; in that case, you should simply loop through the array.

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
1

try this

 JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data[0].id");
System.out.println("id is "+ wID);
Vinayak B
  • 4,430
  • 4
  • 28
  • 58