Reputation: 43
During JMeter testing I have to get an array of JSON first-level keys' names. I.e. from JSON like
{
"name": "Sally",
"address": {
"country": "Kuba",
"city": "Havana"
}
}
I want to get
<name, address>
I'm using Groovy & JsonSlurper, but have no idea, how to get this.
Upvotes: 2
Views: 6578
Reputation: 167992
Put the following code into "Script" area:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).keySet().eachWithIndex { key, index ->
log.info('Key ' + index + ': ' + key)
}
Explanation:
prev
is an instance of SampleResult which provides access to the parent Sampler result objectReferences:
Upvotes: 0
Reputation: 333
If you already have a JSON object, you can do:
println jsonObject.keySet()
If you don't, you will need to create one. With the stringfied JSON you can do:
def json = '{"name": "Sally","address": {"country": "Kuba","city": "Havana"}}'
def jsonObject = new JsonSlurper().parseText(json)
println jsonObject.keySet()
Upvotes: 4