0

Hope this question finds you all in good health.

As per title, would like to know how this is done in Groovy. Found a few, such as this, but the question and answer did not help.

The JSON is like this

def ​json = '''{
     "boston": [
{
  "name":"bob",
  "phone":"242 123123",
},
{
  "name":"alice",
  "phone":"212-123-345",
}
],
"chicago": [
{
  "name":"charlie",
  "phone":"313-232-545",
},
{
  "name":"denise",
  "phone":"414-123-546",
}
]
}'''

But how do I use the value, for example bob to get boston?

When you use parsedjson['chicago']['email'], the result would be

[charlie@chicago.com, denise@chicago.com]

I tried to do something like

def getKey = parsedjson['email']?.key

as suggested here but in JIRA ScriptRunner console returned null

Any pointer is greatly appreciated in advance!

Iqlaas Ismail
  • 385
  • 7
  • 21

1 Answers1

1

parsedjson['email']?.key returned null because key is not a List method. key is an Entry method so to find the key from a value you have to iterate through the Map's Entry Set.

Here's an example to get the city from the person's name using Map.find which returns an Entry:

parsedjson.find { it.value.find { it["name"] == "bob" } }.key
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29