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]')
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]