0

Using com.fasterxml.jackson.databind.JsonNode and the .at(JSONPointerExperssion) method I am getting JSON nodes and values out of JSON.

I can get my objects & nodes. I can read values just fine. But I'm trying to read a specific value from the last element in a JSON Array using a JSON Pointer Expression. Can this be done?

Example:

{
    "Items": [
        {
            "itemId": 123
        },
        {
            "itemId": 456
        }
    ]
}

Reading https://datatracker.ietf.org/doc/html/rfc6901 I see I can get specific array elements by their index, ie:

/Items/0/itemId evaluates to 123

/Items/1/itemId evaluates to 456

Does anyone know how to write a JSON Pointer Expression that will refer to the last element?

I've tried a few things including:

/Items/-1/itemId

(Yes I know specs say using - will give you an error, but ya gotta try )

dhsearl
  • 1
  • 1
  • Last element is not supported yet. I suggest to use [JsonPath](https://github.com/json-path/JsonPath) library. Take a look at some examples: [Iterate over a large JSON Array with JSONPath](https://stackoverflow.com/questions/55366515/iterate-over-a-large-json-array-with-jsonpath/55367535#55367535), [Create Java Array from one element of JSONArray](https://stackoverflow.com/questions/54773864/create-java-array-from-one-element-of-jsonarray/54774576#54774576) – Michał Ziober Sep 30 '21 at 09:01

0 Answers0