hnvasa
hnvasa

Reputation: 860

How to retrieve a value from an array inside of a JSON object using Jackson

Simply put, how do I retrieve {"value1":123"} using Jackson in a non-chaining way?

{
  "aaa": [
      {
        "value1": "123"
      }
  ],
  "bbb": [
      {
          "value2": "456"
      }
  ]
}

I tried using: jsonNode.at("/aaa[Array][0]) but i get missing node in response.

Any help would be good.

Upvotes: 5

Views: 6336

Answers (3)

Cristian Chereji
Cristian Chereji

Reputation: 96

Using node.path("aaa").get(0) is what retrieved the first item from array. Any other ideas like node.path("aaa[0]") or node.path("aaa/0") do not work.

Upvotes: 1

S.K.
S.K.

Reputation: 3677

The correct json path expression would be "/aaa/0/value1"

Use:

jsonNode.at("/aaa/0/value1")

Upvotes: 9

Ashok Kumar N
Ashok Kumar N

Reputation: 573

use this below code :

    JsonNode node = mapper.readTree(json);
    System.out.println(node.path("aaa").get(0)); // {"value1":"123"}
  1. use jackson-databind.
  2. use this

    node.path("aaa").get(0).get("value1") // 123.

Upvotes: 1

Related Questions