I'm using the Rest-Assured framework to read and verify responses from a REST service. I have the following json blob, returned from a POST call, and I need to extract the value for "chatNumber":
{
"authorized": true,
"guest": {
"chatNumber": 371442,
"username": "billyBob",
"guestId": 37561,
"primary": true,
"coppaRestricted": false
}
}
The json response has been saved to an object called "Response".
I want to do something like:
String myValue = Response.path("chatNumber");
But it's not working. path() can't find "chatNumber". There's syntax that I'm apparently missing to get the "chatNumber" value from within the "guest" list of items. I'm not sure I'm even using the correct language to describe "guest"...
What must I do to get the value of "chatNumber" from the json blob above?
Any help here would be lifesaving.
Thanks,
Wulf
So, with the help of better Devs than I here at work, this is how to get the value of chatNumber in the above json blob:
Use the following imports:
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.path.json.JsonPath;
import static com.jayway.restassured.RestAssured.given;
With the json response blob listed above (called Response) already being captured after a REST POST call, do this:
String myBlob = Response.asString();
int chatNumber = JsonPath.with(myBlob).get("guest.chatNumber");
Then if we print the contents of chatNumber at runtime like so:
System.out.println(chatNumber);
we get:
371442
Two days of my life spent trying to figure this out. Ugh....
Hopefully someone else gets a little help from my suffering. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With