Sandy Garrido
Sandy Garrido

Reputation: 196

Unable to get a key value called properties "properties": "Value" Groovy

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

Upvotes: 2

Views: 434

Answers (2)

Ori Marko
Ori Marko

Reputation: 58862

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

Upvotes: 1

Conrad37
Conrad37

Reputation: 175

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']

Upvotes: 0

Related Questions