Rest Json Response, Need to get all the attribute key in the same order as in the response
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Rest Json Response, Need to get all the attribute key in the same order as in the response
Hi, I am new to this community, I am working on a testing project and I use soap ui free version.
I have a Json response with around 64 attributes in a set, I need to get all the keys in the same order as in the response and verify all the sets have the same order.
Example:
{
"responseCode":200,
"responseData":
"Header":
"HeaderDate":["ABC1"
"ABC2"
"ABC3"
"ABC4"
to "ABC65"]
"DataValue":[
"ABC1":"Value",
"ABC2":"Value",
"ABC3":"Value",
to "ABC65":"Value"]
[
"ABC1":"Value",
"ABC2":"Value",
"ABC3":"Value",
to "ABC65":"Value"]
this can bring in even 2000 or more sets
I have to get all the Keys in the order in the response and validate with the sequence in the HeaderData
If I use keySet() I get in the unordered list.
Please guide me
Thanks in advance
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can get an ordered list with collect:
ArrayList<String> keys = [b:2, a:1].collect { key, value -> key }
assert keys == ["b", "a"]
assert keys != ["a", "b"]
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your quick response, I will try and let you know,
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am new to groovy script, Please can you explain what is [b:2,a:1], Thank you in advance
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's a Map. It's just like an object in JSON. So for parsing a response, and checking the keys of the object, you would do something like this:
String response = '''{ "DataValue": [ { "ABC1": "Value", "ABC2": "Value" }, { "ABC1": "Value", "ABC2": "Value" } ] }''' new groovy.json.JsonSlurper().parseText(response)["DataValue"].each { dataValue -> ArrayList<String> keys = dataValue.collect { key, value -> key } assert keys == [ "ABC1", "ABC2" ] }
And the assertion will fail if the order is different to what you are expecting.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your response, Appreciate your help
