Forum Discussion

SuperSingh's avatar
SuperSingh
Contributor
7 years ago

Extracting child node from JSON Response

I have Metadata in my response that contains nodes & child nodes.

I am looking at way to extract child node & its value using groovy but not able to find.

 

Below is the sample metadata response.

I am trying to extract node "Item 1" and its associated value from the response for assertions.

 

Greatly appreciate any kind of help here !

 

<a: MetaData>
[
{
"Type": "SectionList",
"ID": "subsections",
"LabelText": "GradeItem",
"Visible": "true",
"Selection": 0
},
{
"Type": "DropDownList",
"ID": "PackageType",
"LabelText": "Package",
"SubType": "Coverage",
"Required": "False",
"OptionValues": {
"0": "Silver",
"1": "Gold",
"2": "Platinum"
},
"Visible": "inherit",
"DataTarget": "Info",
"Value": "0",
"Selection": 0
},
{
"Type": "ItemList",
"ID": "SuperItems",
"SubType": "ActionItem",
"OptionValues":
{
"Item 1": "-$10",
"Stock 2": "-$20",
"Element 1": "-$30",
"Item 4": "-$40",
"Row 6": "-$50",
"Total Items": "-$150"
},
"Visible": "true",
"Selection": 0
}]</a: MetaData>

 

 

  • Script assertion with jsonslurper or jsonpath.

    See http://goessner.net/articles/JsonPath/

     

    import com.eviware.soapui.support.XmlHolder
    def holder = new XmlHolder(context.Response)
    def Metadata = holder["//*:MetaData"]
    //log.info Metadata
    
    import static com.jayway.jsonpath.JsonPath.parse
    log.info parse(Metadata).read('$.[?(@.Type==ItemList)].OptionValues.Item 1[0]')
  • PaulMS's avatar
    PaulMS
    7 years ago

    That sounds like a job for JsonSlurper

    http://docs.groovy-lang.org/latest/html/documentation/#_processing_json

     

    If the name Item 1 and value are not constant how would you identify the expected result for assertions? Is it the first name in OptionValues? This code could help

     

    import com.eviware.soapui.support.XmlHolder
    def holder = new XmlHolder(context.Response)
    def Metadata = holder["//*:MetaData"]
    //log.info Metadata
    
    import groovy.json.JsonSlurper
    def json = new JsonSlurper().parseText(Metadata)
    def map = json.OptionValues[2]
    log.info map.entrySet()
    log.info map.entrySet()[0]
    log.info map.keySet()[0]
  • PaulMS's avatar
    PaulMS
    Super Contributor

    Script assertion with jsonslurper or jsonpath.

    See http://goessner.net/articles/JsonPath/

     

    import com.eviware.soapui.support.XmlHolder
    def holder = new XmlHolder(context.Response)
    def Metadata = holder["//*:MetaData"]
    //log.info Metadata
    
    import static com.jayway.jsonpath.JsonPath.parse
    log.info parse(Metadata).read('$.[?(@.Type==ItemList)].OptionValues.Item 1[0]')
    • SuperSingh's avatar
      SuperSingh
      Contributor

      Bang On ! Thank You So Much PaulMS !

      Ran your query and I get what I needed. Your query is so precise & to the point without much hussle :D

      Really Appreciate it.

       

      Best,

      Predator .

    • SuperSingh's avatar
      SuperSingh
      Contributor

      PaulMS

      Quick Question : You gave the query to pick  the value of the node .How do I get the node itself ?

      E.g. The query returns the value of "Item 1" . But How do I extract "Item 1" , "Stock 2" .?

       

      Thanks in Advance !

       

      Best,

      Predator

      • PaulMS's avatar
        PaulMS
        Super Contributor

        Do you want to return all OptionValues?

         

        JSONPath '$.[?(@.Type==ItemList)].OptionValues.[0]' result is 

        {"Item 1":"-$10","Item 4":"-$40","Total Items":"-$150","Stock 2":"-$20","Row 6":"-$50","Element 1":"-$30"}

  • use JSONSlurper to parse values. You can try something like this

     

    import groovy.json.JsonSlurper

    payload = testRunner.testCase.getTestStepByName("REST Service").getPropertyValue("response")
    slurperResponse = new JsonSlurper().parseText(payload)
    node = (slurperResponse.first()) //first is a method that will get the first node

     

    • SuperSingh's avatar
      SuperSingh
      Contributor

      Thanks bagochips for the reply.

      I was looking to pick up a nested node that is within a node.I was able to get it from PaulMS query.

       

      Thanks for your time. Appreciate it :)

       

      Best,

      Predator