How to extract & validate nodes from JSON which is part of XML response
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to extract & validate nodes from JSON which is part of XML response
I have response in XML format that contains metadata (which is in JSON).I need to extract the values from JSON and validate them.
Can someone please help me with script to extract the JSON nodes .
Below is the sample layout of my XML response.
I need to extract "Name1" from the response and use it for validation/assertions.
<ApiRootContext xmlns="Link1" xmlns:i="Link2">
<ApiHeader xmlns:a="Link3">
<Duration>500</Duration>
<Status>InProgress</Status>
</ApiHeader>
<Metadata>
{
"Type": "Type1",
"ID": "Name1",
"Label": "Text1",
},
{
"Type": "Type2",
"ID": "Name2",
"Label": "Text2",
}
</Metadata>
<city>Boston</city>
<state>MA</state>
<country>US</country>
</ApiRootContext>
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Script assertion would be
import com.eviware.soapui.support.XmlHolder def holder = new XmlHolder(context.Response) def Metadata = holder["//*:Metadata"] log.info Metadata import groovy.json.JsonSlurper def slurper = new JsonSlurper() def json = slurper.parseText Metadata log.info(json.ID[0])
However your sample XML response seems to include invalid JSON so I changed the last line to
log.info(json.ID)
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot ! Works out perfectly .
Appreciate your time & response 🙂
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have one more question here, associated with the same post ? Sorry to not add it earlier.
How can I do a search for the value if the response data is not in sequence.
For. Eg. if this is my Metadata,
<Metadata>
{
"Type": "Type3",
"ID": "Name3",
"Label": "Text3",
},
{
"Type": "Type2",
"ID": "Name2",
"Label": "Text2",
}
{
"Type": "Type2",
"ID": "Name2",
"Label": "Text2",
}
And I have to extract value of "Label" for "Type3" , how do I do that. (I cannot set it based on json.ID[0] coz the order might vary)
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You may need to contact your service provider / your team if it is internally developed.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To make json valid, it should be like below (note the square brackets around as you are getting an Json Array)
<Metadata> [{ "Type": "Type1", "ID": "Name1", "Label": "Text1", }, { "Type": "Type2", "ID": "Name2", "Label": "Text2", }] </Metadata>
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My bad , I did not add the square brackets in there.
It is a valid JSON. Can you please share the script .
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Use below script:
https://github.com/nmrao/groovyScripts/blob/master/json/QueryJsonArray.groovy
use "log.info" instead of "println" in the above script.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Awesome ! This is what I was looking for.
Thanks a ton @nmrao . You made my day ! 🙂
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
