Forum Discussion

SuperSingh's avatar
SuperSingh
Contributor
8 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. ...
  • PaulMS's avatar
    8 years ago

    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
    8 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]