Ask a Question

Parse this json response

Double_Akins
New Contributor

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.

7 REPLIES 7
richie
Community Hero

Re: Parse this json response

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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
richie
Community Hero

Re: Parse this json response

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 

 

escapedjsonproblem.PNGto the following:

escapedjsonproblem2.PNG

 

 

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

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Double_Akins
New Contributor

Re: Parse this json response

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

richie
Community Hero

Re: Parse this json response

it's all good - I only know about that particular event handler cos someone on this forum told me!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Double_Akins
New Contributor

Re: Parse this json response

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.

2018-09-20_0843.png2018-09-20_0845.png

 

Thanks,

Double A 

 

 

richie
Community Hero

Re: Parse this json response

Erm....yeah....i know the response is json...im lost...why do you think i think its xml? Cos i dont! 🙂

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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
richie
Community Hero

Re: Parse this json response

Right i can see we've misunderstood each other. I wasnt saying add what i put in a groovy step (although you can). All your responses are json and they all have escaped json in an eventmessages object...right?

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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
cancel
Showing results for 
Search instead for 
Did you mean: