0

I'm trying on JSONPath expression on https://jsonpath.com. I have the following JSON, and I only need the last Order's (tranTxt) Invoice number (invcNr). So first I filtered out the object where I have the status Order, then I wanted the get only the last Order's Invoice number.

This is the JSON:

[
    {
        "tranDt": "2022-06-15",
        "tranSeqNr": 676351522,
        "tranTxt": "Order",
        "invcNr": "73811514"
    },
    {
        "tranDt": "2022-06-30",
        "tranSeqNr": 710063627,
        "tranTxt": "Pending"
    },
    {
        "tranDt": "2022-08-11",
        "tranSeqNr": 795465799,
        "tranTxt": "Order",
        "invcNr": "73980890"
    },
    {
        "tranDt": "2022-08-17",
        "tranSeqNr": 806178419,
        "tranTxt": "Pending"
    }
]

My JSONPath:

$.[?(@.tranTxt=="Order")]

The result, is what I wanted:

[
  {
      "tranDt": "2022-06-15",
      "tranSeqNr": 676351522,
      "tranTxt": "Order",
      "invcNr": "73811514"
  },
  {
      "tranDt": "2022-08-11",
      "tranSeqNr": 795465799,
      "tranTxt": "Order",
      "invcNr": "73980890"
  }
]

And here I stucked, to get the last invoice no. "invcNr": "73980890"

I know that usually to use $.[-1:] I would get the last array element. But here is not helping.

I tried to use this JSONPath: $.[?(@.tranTxt=="Order")].invcNr And the result is

[
  "73811514",
  "73980890"
]

So how can build the $.[-1:] part into $.[?(@.tranTxt=="Order")] to receive the last order's invoice number?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Zoltan
  • 15
  • 4
  • You can't do it with one single `json-path-expression`. You have to do it in two steps where you provide the output of the first as an input to the second. – Akshay G Oct 07 '22 at 07:39

1 Answers1

0

What was working fine for me here is:

$..[-1:]

enter image description here

Or you can get your tranSeqNr as: $..[-1:].tranSeqNr

enter image description here

Or if you want to combine multiple:

$.[?(@.tranTxt=='Order' && @.tranSeqNr-1)].invcNr

you get:

[
  "73811514",
  "73980890"
]

Not sure this is what you need.

vlatko606
  • 909
  • 1
  • 13
  • 21