How to retrieve values from nested json structures (Maps)

Looking for some help here.

I’m writing an api test and I’m trying to find out what the value of a particular field is.

[data:[active:true, biometricsEnabled:true, deactivated:false, disabled:false…]]

See that biometricsEnabled:true field? Yeah, that. I’m trying to pull that value bc at some point I want to validate that it is correctly set.

But, using:

WS.sendRequest(findTestObject('APIs/API Login'))

ResponseObject ro = WS.sendRequest(findTestObject('APIs/API Settings'))

String response = ro.getResponseText()

JsonSlurper slurper = new JsonSlurper()

Map parsedJson = slurper.parseText(response)

println parsedJson

boolean biometrics = parsedJson.get("biometricsEnabled").asBoolean()
 
println ("biometrics enabled: " + biometrics)

The println gives me ‘false’ even if the setting is true.

So, I’m doing something wrong here. Any help is appreciated!!

Try this, Amanda…

Map parsedJson = slurper.parseText(response)
boolean biometrics = parsedJson["biometricsEnabled"]
println ("biometrics enabled: " + biometrics)

I mocked this using my system:

tried that. Still not getting the right response:

image

Map parsedJson =  (Map) new JsonSlurper().parseText(response)
boolean biometrics = parsedJson["biometricsEnabled"]
...

One more thing, the structure you posted:

[data:[active:true, biometricsEnabled:false, deactivated:false, disabled:false…]]

is not legal JSON*. All property names should be strings - "data": ["active": ...] etc. But I don’t know how stringent JsonSlurper is about these things.

That response is from the slurper. I have it set to println just so I can see it. So, that’s post slurping (hahaha). I’ll try that (Map) bit there

Ah right. I’ve seen that. From a debugging perspective, that’s seriously annoying. And ridiculous.

Annoying and ridiculous is my middle name. :smiley:

Bad news, still not matching.

println another property - preferably a string. Does that work? If not, the JSON is likely “bad”.

Actually, no I don’t. It’s false!

HAHA! Good catch. Fixed it.

But, you might be right. If I change the line to

String biometrics = parsedJson["email"]

and try to println the email (biometrics) I get null.

So, this is fun

Set a breakpoint on the line after parsedJson is assigned:

Map parsedJson = (Map) new JsonSlurper().parseText(response)
// breakpoint here

Run the app in debug mode, then go to the Expressions panel and add a reference to parsedJson (you’ll need to wait while the debugger thinks about your highly complex and very unusual request to view the content of a groovy variable :yawn:)

Here’s mine (for TCJSON):

never use getResponseText if you intend to parse it / map it the groovy way
use getResponseBodyContent. the text one will always return a string, so you have to parse it as a string

so, changed it to the getResponseText() and put in breakpoints

image

I feel like something is still not right - the println is still coming back false when it should be true

because a string with value ‘true’ casted as boolean will be always false. since it is not a boolean True

your json response is malformed for some reason…not following parsing standards. therefore, the parser will pick as plain string

soooo…how do I pull the value of that then?

you may have to write certain code…if ‘true’ then True. note the ‘T’

Not true. Only an empty string will be false:

    boolean thing = (boolean) "true"
    println(thing) // true
    boolean thing = (boolean) "false"
    println(thing) // true
    boolean thing = (boolean) ""
    println(thing) // false

Open the values member:

image

You have five values stored. You need to watch for red herrings here :confused:

image