there is a way to get all keys and related values of a json? I don't know the fields in output, then I can't use json extractor. I've found this code, but it returns only the keys:
import groovy.json.JsonSlurper
def traverse
traverse = { tree, keys = [], prefix = '' ->
switch (tree) {
case Map:
tree.each { k, v ->
def name = prefix ? "${prefix}.${k}" : k
keys << name
traverse(v, keys, name)
}
return keys
case Collection:
tree.eachWithIndex { e, i -> traverse(e, keys, "${prefix}[$i]") }
return keys
default :
return keys
}
}
def content = new JsonSlurper().parseText(prev.getResponseDataAsString())
traverse(content).each { println it }
Output:
....
devices[24].creationDate
devices[24].updateDate
devices[24].attributes
devices[24].attributes.name
devices[24].attributes.location
devices[24].attributes.address
devices[24].attributes.latitude
devices[24].attributes.longitude
devices[24].attributes.brand
devices[25].deviceId
devices[25].deviceStatus
devices[25].deviceType
devices[25].creationDate
devices[25].updateDate
devices[25].attributes
....
could you help me to retrive keys and values?
Thanks in advance
It's not very clear how does your JSON look like and what form of the output do you expect.
It might either be as simple as:
or you might want to amend your code to include attribute value if it's not a JSON Object, something like:
More information: