How do I get a value from a JsonNode array?

4.6k Views Asked by At

I have such a json response:

"data": [
        {
            "id": "1",
            "login": "name",
            "display_name": "dName",
            "type": "",
            "broadcaster_type": "",
            "description": "",
            "profile_image_url": "...",
            "offline_image_url": "",
            "view_count": 1,
            "created_at": "2017-09-11T20:36:17Z"
        }
    ]

I get it using a WebClient get request:

dateRequest
                .execute(sendBy)
                .log()
                .map(jsonNode -> {
                    String createdAt = "";
                    
                    return createdAt;
                })
                .subscribe();

And inside the map I want to pull out the creation date.The function says that it is an array, but it cannot be iterated. That is JsonNode data = jsonNode.path("data"); writes everything into one solid value:

{"id":"","login":"name","display_name":"dName","type":"","broadcaster_type":"","description":"","profile_image_url":"...","offline_image_url":"","view_count":1,"created_at":"2017-09-11T20:36:17Z"}

And it is not possible to pull out the value normally , that is , calls data.path("id").textValue() return null.

The only way that turns out to use:

Iterator<JsonNode> iterator = data.iterator();
JsonNode next = iterator.next();
JsonNode id = next.path("id");
String stringId = id.textValue();

Is it possible to pull out these values normally without using an iterator for 1 value?

1

There are 1 best solutions below

0
On

As you said its an Array. Cast the node to an ArrayNode. Select the first element [0] and call the getter. Should be it. Or check out this: https://stackoverflow.com/a/16793133/15565539