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?