0

I am trying to get property name in JSON response object in Java Spring Boot using JsonPath. I want to acces first (or second) property name, but I don´t want to hardcode them.

I find out that when I use * wildcard, selecting multiple nodes is not supported.

JSON format is:

{
   "item1": [
     {...},
     {...},
     {...},
    ],
   "item2": [
     {...},
     {...},
     {...},
    ],
}

Do you have any idea, if this is posible?

Thank you

Petr Jelínek
  • 1,259
  • 1
  • 18
  • 36

2 Answers2

1

Yes. What you want to do is iterate over the keys of the JsonObject

JSONObject jsonObject = new JSONObject(contents.trim());
Iterator<String> keys = jsonObject.keys();

while(keys.hasNext()) {
    String key = keys.next();
    if (jsonObject.get(key) instanceof JSONObject) {
          // do something with jsonObject here      
    }
}

Reference : https://stackoverflow.com/a/10593838/7622687

NiksVij
  • 183
  • 1
  • 9
0

I figured it out - I didn't use response from endpoint. I used output of the method that was as a endpoint handler. And then I used @NiksVij's solution above.

Thanks!

Petr Jelínek
  • 1,259
  • 1
  • 18
  • 36