I've encountered a JSON structure like the following:
Note that the params element contains a list of similar sub elements but it is not represented as an array as you would expect. The names of those sub elements cannot be predicted so we can't query them explicitly.
I want to iterate through the sub elements using the Path class in a manner that allows me to access the name of the element (e.g. "firstName", "surname") but don't see an obvious way to do that. The following code shows what I'd like to achieve but doesn't compile as there is no attribute called name on the Path object.
The result should be output as follows:
Anyone got advice on how to achieve this?
Found the answer thanks to Radoslav Ivanov
Wouldn't just plain 'ol Groovy be neater?
Gives you this output:
And you only need to manage the name of the outer map.
Actually the reason I was looking at the Path class is because I was looking for an alternative to JsonSlurper as it re-orders the parameters alphabetically according to their key which was entirely undesirable for my application. Not sure why it does this and there doesn't seem to be a way to turn it off.
In the example JSON_DATA above if you put type above label you will still get the same output with type appearing after label in the output.