Forum Discussion

Double_Akins's avatar
Double_Akins
New Contributor
7 years ago

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.

  • richie's avatar
    richie
    Community Hero

    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

    • richie's avatar
      richie
      Community Hero

      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

       

       

      • Double_Akins's avatar
        Double_Akins
        New Contributor

        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 

         

         

    • Double_Akins's avatar
      Double_Akins
      New Contributor

      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's avatar
        richie
        Community Hero

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

         

        richie