Parse this json response
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Parse this json response
I am stuck. I am new to groovy scripting. How do I parse out theOrchestrationId, CenterId and AddressId of the below response? All are unique, dynamic and generated in the application. I need these Ids to do other REST apis.
RESPONSE
{
"events" : [
{
"eventTypes" : "ICenterCreatedEvent",
"message" : "{\"CenterName\":\"National League\",\"ClaimName\":\"League of Nations\",\"CenterGroup\":\"Samples\",\"CliaNumber\":\"23456789019\",\"Phone\":\"4043495000\",\"Fax\":\"4045345000\",\"Manager\":\"Bruce Wayne\",\"Npi\":\"23456789019\",\"TaxId\":\"678227878\",\"BillingCode\":\"71\",\"Active\":true,\"Address1\":null,\"Address2\":null,\"City\":null,\"State\":null,\"Zip\":null,\"Zip4\":null,\"PlatformId\":\"07674247-be80-4b64-91dc-29c353bd8c09\",\"PlatformEventId\":\"0aad48a6-3112-4e44-82ad-04128a2219a8\",\"TimeStamp\":\"2018-09-19T13:57:53.9595403+00:00\",\"OrchestrationId\":\"618a4887-11b7-472f-9699-07b3fe328ead\",\"IdentityContext\":\"BogusIdentityContext\"}",
"timeStamp" : "2018-09-19T13:57:53.9595403+00:00"
},
{
"eventTypes" : "ICenterAddressCreatedEvent",
"message" : "{\"CenterId\":\"07674247-be80-4b64-91dc-29c353bd8c09\",\"AddressId\":\"09b83092-e17a-44dc-a3c4-1defb90aa925\",\"Address1\":\"123 Caoco Drive\",\"Address2\":\"Suite 250\",\"AddressType\":\"NotSpecified\",\"City\":\"Marietta\",\"State\":\"GA\",\"Zip\":\"30339\",\"Zip4\":\"5643\",\"PlatformEventId\":\"b6e53ea2-03d5-4cd0-bf57-520bf285fee4\",\"TimeStamp\":\"2018-09-19T13:57:54.4283015+00:00\",\"OrchestrationId\":\"618a4887-11b7-472f-9699-07b3fe328ead\",\"IdentityContext\":\"BogusIdentityContext\"}",
"timeStamp" : "2018-09-19T13:57:54.4283015+00:00"
}
]
}
Thanks in advance.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
You've got escaped json which isn't being read by ReadyAPI!s json parser which is why you cant use the embedded functionality to grab the values you want.
You have a couple of choices here - theres an RequestFilter.afterRequest event handler (published in soapui.org) that is pretty handy - for altering responses accordingly.
the script to remove CDATA tag handlers from xml is as follows:
def content = context.httpResponse.responseContent content = content.replaceAll( "<!\\[CDATA\\[", "" ) content = content.replaceAll( "]]>", "" ) //log.info( content ) context.httpResponse.responseContent = content
So - click on the events tab and add a RequestFilter.afterRequest handler.
Set its content to something like the following - you just need to alter this to remove the backslashes from your responses
def content = context.httpResponse.responseContent content = content.replaceAll( "\\\\\", "" ) //log.info( content ) context.httpResponse.responseContent = content
I think that's it - I always get confused how many escapes I need to do this - you might need to play with the number of backslashes in the script - but thats the basis of what you need - add that event handler in - and it should alter the content of your responses removing the backslash resulting in the response 'appearing' as non-escaped .json in your outline tab in the response resulting in you being able to use the embedded functionality to correlate/grab the dynamic session variables using normal property transfer.
hope this helps,
richie
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
aah - I just noticed you'll need to remove 2 instances of the double quotes on top of what I've already provided. So after running the above script I mentioned to remove the '\' chars, you'll also need to remove the " chars opening and closing the message object's contents.
Changing the below snippet
to the following:
so you need to update what I said before to something like the following:
def content = context.httpResponse.responseContent content = content.replaceAll( "\\", "" ) // to replace the '\' char with blank content = content.replaceAll( "\"{", "{" ) // to replace the "{ to { content = content.replaceAll( "\"}", "}" ) // to replace the "} to } log.info( content ) context.httpResponse.responseContent = content
As I stated before - the json escaping confuses me - I've just read up a little and it isn't as confusing as I thought - so I think I've got the escaping correct now - but what you need is something pretty close to above.
cheers,
richie
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Richie,
Thank you for explaining this to me. I had not seen this anywhere before and did as much as I knew to do by parsing the response.
Thanks a million.
Double A
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
it's all good - I only know about that particular event handler cos someone on this forum told me!
richie
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Richie,
The response is returning in JSON, not XML. This application only sends and accepts json. I am not sure of what is wrong with the script in line 7. I have attached the request and response and the script you provided.
Thanks,
Double A
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hang on....i'm travelling at the moment.....give me half hour and i'll look at your response properly....i cant see properly using my phone
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As this escaped json is going to occur for all your responses im guessing i said use an events handler instead. Thst is, click on the Events button below the maintoolbar (next to Preferences button), click on the green plus and select RequestFilter.afterRequest.
Then add the code i gave you in the input field below. What this will do is alter the response for every request in your project so youll never get escaped json in your responses.....so you wont need any further groovy allowing you to rely on the OOB functionality to assert and property transfer.
Im typing this out on my phone so i cant see your last post but i remember you having an import declaration and another line where you'd parsed the response that you dont need with the event handler content.
Home in 20mins
Cheers
Rich
