Sorry for the bad topic title!
I am struggling to get the “field_set_name” from the json below using getElementPropertyValue. The first json object ending in -1353 is dynamic and will probably be different each time I make my request and of course matches the value of key “field_set_name”, but I can’t work out to get either. Happy to take the object or the key value to be honest.
{
"/api/objects/field_set_names/metadatafieldsetname-1353": {
"is_general": true,
"is_global": false,
"name": "General",
"field_set_name": "/api/objects/field_set_names/metadatafieldsetname-1353"
}
}
Have tried a number of things like this but no joy as yet:
String generalFieldSetId = WS.getElementPropertyValue(response, ‘[0]’, FailureHandling.STOP_ON_FAILURE)
AND
String generalFieldSetId = WS.getElementPropertyValue(response, ‘[0].field_set_name’, FailureHandling.STOP_ON_FAILURE)
Obviously ‘[0]’ isn’t the correct thing to use in the instance.
Any thoughts appreciated. I will post back in the mean time should I resolve this if my brain ever decides to start working…
This requirement makes your problem hard to solve. WS.getElementPropertyValue() is short for you.
You need to write a code as complicated as the following:
import groovy.json.JsonSlurper
String json = '''{
"/api/objects/field_set_names/metadatafieldsetname-1353": {
"is_general": true,
"is_global": false,
"name": "General",
"field_set_name": "/api/objects/field_set_names/metadatafieldsetname-1353"
}
}'''
JsonSlurper slurper = new JsonSlurper()
Object obj = slurper.parseText(json)
assert obj != null
String fieledSetName = ''
if (obj instanceof Map) {
Map root = (Map)obj
root.keySet().forEach({ String key ->
if (key.startsWith("/api/objects/field_set_names/metadatafieldsetname")) {
Map m = root[key]
assert m != null
fieldSetName = m.field_set_name
}
})
} else {
throw new IllegalStateException("expected a Json Object, but encountered with something unexpected")
}
println "field_set_name is: ${fieldSetName}"
When I ran this, I got the following output in the console:
2021-03-22 21:44:16.545 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2021-03-22 21:44:16.551 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/TCd
2021-03-22 21:44:17.626 DEBUG testcase.TCd - 1: json = "{
"/api/objects/field_set_names/metadatafieldsetname-1353": {
"is_general": true,
"is_global": false,
"name": "General",
"field_set_name": "/api/objects/field_set_names/metadatafieldsetname-1353"
}
}"
2021-03-22 21:44:17.658 DEBUG testcase.TCd - 2: slurper = new groovy.json.JsonSlurper()
2021-03-22 21:44:17.707 DEBUG testcase.TCd - 3: obj = slurper.parseText(json)
2021-03-22 21:44:17.723 DEBUG testcase.TCd - 4: assert obj != null
2021-03-22 21:44:17.732 DEBUG testcase.TCd - 5: fieledSetName = ""
2021-03-22 21:44:17.738 DEBUG testcase.TCd - 6: if (obj instanceof java.util.Map)
2021-03-22 21:44:17.739 DEBUG testcase.TCd - 1: root = obj
2021-03-22 21:44:17.740 DEBUG testcase.TCd - 2: keySet().forEach({ java.lang.String key -> ... })
2021-03-22 21:44:17.797 DEBUG testcase.TCd - 8: println(field_set_name is: $fieldSetName)
field_set_name is: /api/objects/field_set_names/metadatafieldsetname-1353
2021-03-22 21:44:17.859 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TCd
If you could use some JSON Path library (for example Jayway JsonPath), then you can write a code a bit simpler by using Filter operation with a Predicate by Regular Expression.
@kazurayam - Thank you . I was just trying something very similar with JsonSlurper when I saw your response. Works a treat. Will turn in to a metthod as I can reuse for a number of requests 
Thank you very much.