15

I have json string like below

[
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 0
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 1
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 2
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 3
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 4
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 5
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "test",
        "partition": 0,
        "offset": 6
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "Hi Test",
        "partition": 0,
        "offset": 7
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "Hi Test",
        "partition": 0,
        "offset": 8
    },
    {
        "topic": "inputTopic",
        "key": "0",
        "message": "Hi Test",
        "partition": 0,
        "offset": 9
    }
]

How can I get last item using jsonpath expression?

 {
    "topic": "inputTopic",
    "key": "0",
    "message": "Hi Test",
    "partition": 0,
    "offset": 9
}
2
  • We can get first element by using expression like this " $.[1] " . Commented Aug 22, 2015 at 8:30
  • What have YOU tried so far? Share your findings / code. Commented Feb 18, 2019 at 15:23

3 Answers 3

37

As you can read in the docs you can ether do a

$..book[(@.length-1)]

as mentioned by Duncan above. Or you can

$..book[-1:]

Personally I find the first a bit more expressive, the latter a bit smarter to write. The latter also seems to be the intended default. I'd say it's a question of personal flavor.

Sign up to request clarification or add additional context in comments.

1 Comment

Even more convenient is "$..book[-1]" which returns just the last element, whereas "$..book[-1:]" returns an array containing the last element.
10

You can use the underlying language features when using JsonPath, so in JavaScript for example, the length property is available. You can therefore say I want the last one by asking for length minus one.

Assuming you are using JsonPath from JavaScript the following query will find the last item:

$.[(@.length-1)]

You can see the query working here on this online JsonPath tester: http://www.jsonquerytool.com/sample/jsonpathlastinarray

Comments

3

What was working here for me was:

$..[-1:]

enter image description here

You can test it here: https://jsonpath.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.