How to retrieve values from nested json structures (Maps)

json

#1

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!!


#2

Try this, Amanda…

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

I mocked this using my system:


#3

tried that. Still not getting the right response:

image


#4
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.


#5

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


#6

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


#7

Annoying and ridiculous is my middle name. :smiley:

Bad news, still not matching.


#8

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


#9

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


#10

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


#11

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):


#12

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


#13

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


#14

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


#15

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


#16

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


#17

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


#18

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

#19

Open the values member:

image

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


#21

image