0
votes

I need to extract the last element from my json response, i am using json post processor.

json Path -

$.self[(@.length-1)].cashCardId

After using this path, i got this error -

Exception: Could not parse token starting at position 6. Expected ?, ', 0-9, *

After going through the error, i tried this jsonpath, but it throw "NO MATCH"

$.self['(@.length-1)'].cashCardId

But when i evaluate my jsonPath in an online evaluator, it works fine. Looks like there is any kind of bug in jmeter source, or may be i am producing some syntactical error.

{
    "gift": [
    ],
    "self": [
         {
            "cashInitialAmount": 2000.0,
            "serialNumber": "M79H3LSU",
            "creationTime": 1563783465000,
            "endDate": "2036-01-01",
            "cardType": "ECARD",
            "buyerId": 397,
            "cashPendingAmount": 2000.0,
            "cardSerial": "M79H3LSU",
            "purchaseOrderId": 6123682,
            "initialOffer": 0.0,
            "customerId": 397,
            "offerId": null,
            "cashCardId": 188503,
            "purchaseTime": 1563783465000,
            "lastModified": 1563783465000,
            "activationTime": 1563783465000,
            "cardNumber": "M79H3LSU",
            "cardStatus": "ACTIVE",
            "startDate": "2019-07-22"
        },
        {
            "cashInitialAmount": 2000.0,
            "serialNumber": "2OI5U2UV",
            "creationTime": 1563783382000,
            "endDate": "2036-01-01",
            "cardType": "ECARD",
            "buyerId": 397,
            "cashPendingAmount": 2000.0,
            "cardSerial": "2OI5U2UV",
            "purchaseOrderId": 6123680,
            "initialOffer": 0.0,
            "customerId": 397,
            "offerId": null,
            "cashCardId": 188502,
            "purchaseTime": 1563783382000,
            "lastModified": 1563783382000,
            "activationTime": 1563783382000,
            "cardNumber": "2OI5U2UV",
            "cardStatus": "ACTIVE",
            "startDate": "2019-07-22"
        },
        {
            "cashInitialAmount": 2000.0,
            "serialNumber": "S7EJDE77",
            "creationTime": 1563794985000,
            "endDate": "2036-01-01",
            "cardType": "ECARD",
            "buyerId": 397,
            "cashPendingAmount": 2000.0,
            "cardSerial": "S7EJDE77",
            "purchaseOrderId": 6123723,
            "initialOffer": 0.0,
            "customerId": 397,
            "offerId": null,
            "cashCardId": 188511,
            "purchaseTime": 1563794985000,
            "lastModified": 1563794985000,
            "activationTime": 1563794985000,
            "cardNumber": "S7EJDE77",
            "cardStatus": "ACTIVE",
            "startDate": "2019-07-22"
        }
    ]
}
1
Please add json exampleuser7294900
@user7294900 json is already added in my post, pls check again.Abhinav
seems that there is a bug in json-path jar file, i referred to this link - codota.com/code/java/classes/…Abhinav

1 Answers

0
votes

For whatever reason, the jsonpath syntax works slightly differently for Goessner's jsonpath (JavaScript, https://goessner.net/articles/JsonPath/) vs. the Jayway implementation (Java, https://github.com/json-path/JsonPath)

For one, length is an array property in Goessner's, but in Jayway's its a function, so for e.g. $.self.length will work for Goessner but not for Jayway, you would need $.self.length()

For Jayway's array[-1] will give you the last element. So for your case, the expression $.self[-1].cashCardId should give you what you want.

The different implementations can be tested here: https://jsonpath.herokuapp.com/