Below is the response message (JSON) from an API endpoint.
I need to parse the JSON response, and I need to retrieve the RequestID key.
{
"Status":1,
"RequestID":"29d2d52c-e8fe-447f-9ee1-15e4624be58f",
"Data":[
{
"RegNumber":"ASD3948506",
"CaseData":{
"CaseResult":{
"Message":"success",
"RequestID":"8C15473C0F7771F410928D5D91362B80"
},
"ErrorMessageList":[
]
}
}]
}
Below is my code to retrieve the RequestID
[Inside the CaseResult]
JSONObject actualJson = new JSONObject(response.getResponseText())
def requestID = actualJson.get('Data[0].CaseData.CaseResult.RequestID')
When I executed it, I got the below error message.
org.json.JSONException: JSONObject["Data[0].CaseData.CaseResult.RequestID"] not found.
Could any one provide a solution for this?
3
Answers
You cannot query values using that kind of string:
You will get null as result
Explicit access to the values
Using JsonSlurper you can parse any valid string json to a groovy object.
Jsonpath
This is the only library capable to use string queries to get values from the json:
References
You can not directly query the string from json object. Use below code to get the RequestId.
You may try library Josson. The query syntax is the same as your expected.
https://github.com/octomix/josson