2

I'm making API requests to a service which returns a JSON object within the body.

I can't seem to get the value of a key called "properties" within groovy.

Everytime I call obj.properties i get the following back

{ "class": "org.json.JSONObject" } 

but if I call just the obj I get the expected JSON object

{ "dummy1": , "dummy2": false, "dummy3": etsad, "dummy4": asdfw, "dummy5": qweqwe, "dummy6": 123123, "properties": { "country": UK, } } 

Likewise if I obj.dummy2 i get false it's only when I obj.properties do I get the above mentioned response

3
  • 3
    try obj.get('properties') Commented Jun 11, 2019 at 16:37
  • that worked! amazing, thank you Commented Jun 11, 2019 at 16:44
  • Your json is incorrect format. Use online editors to verify your json: jsoneditoronline.org Commented Jun 12, 2019 at 16:47

2 Answers 2

1

Notice groovy have a special handling for Object's properties, for example for number:

def y = 25 print y.properties 

It will print [class:class java.lang.Integer]

So it's part of basic groovy object

See also an answer about getting non-synthetic properties from groovy object

As @daggett comment, you can use

 obj.get('properties') 
Sign up to request clarification or add additional context in comments.

Comments

0

Check out this answer here on how to access the properties of objects.

The reason obj.properties isn't working is most likely due to the fact that every object will have properties, and in your case obj.properties is getting the properties of the JSON object and not the value associated with the key.

Instead of obj.properties, consider obj['properties']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.