2
votes

I have two requests that return response with similar JSON structure. When I try to use JSON extractor on one, it works properly but when I try to extract value in the same way from the second one, it doesn't work. But let's cut to the chase.

My first response looks like this:

{
    "values": [
        {
            "id": 1,
            "name": "Fendi",
            "logoId": null,
            "belongsToUser": true
        },
        {
            "id": 2,
            "name": "Jean Paul Gaultier",
            "logoId": null,
            "belongsToUser": true
        },
        {
            "id": 3,
            "name": "Nike",
            "logoId": null,
            "belongsToUser": false
        },
        {
            "id": 4,
            "name": "Adidas",
            "logoId": null,
            "belongsToUser": true
        }
    ]
}

And I try to extract ID of the object that "belongsToUser": false in this JSON Extractor:

JSON path expression: values[?(@.belongsToUser == false)].id
Match No.: 0
Default Values: null

And it works perfecty fine.

However, when I try this way on my second response, it doesn't work. The response looks like this:

{
    "values": [
        {
            "id": 12,
            "brandName": "Fendi",
            "productCategoryName": "Shoes",
            "size": "38",
            "colorNames": [
                "color_green"
            ],
            "date": 1536537600000,
            "imageId": null,
            "title": "Money",
            "numberOfOffers": 0,
            "status": "ONGOING"
        },
        {
            "id": 13,
            "brandName": "Fendi",
            "productCategoryName": "Shoes",
            "size": "38",
            "colorNames": [
                "color_green"
            ],
            "date": 1536537600000,
            "imageId": null,
            "title": "Exchange",
            "numberOfOffers": 0,
            "status": "ONGOING"
        }
    ]
}

I try to get id of object that has title variable = "Money" with JSON extractor:

JSON path expression: values[?(@.title == 'Money')].id
Match No.: 0
Default Values: null

But it doesn't find id value and sets my JMeter variable to null. I also tried to leave Money unquoted or in double quotes and tried different JSON path expresions, like

$.values[?(@.title == 'Money')].id
$..[?(@.title == 'Money')].id
$.[?(@.title == 'Money')].id

But none of these seems to work. Do you have any idea how my JSON path expression shoud look to work properly? And why doesn't it work in second case when it works in first? Is it because objets in second response have inside array?

2
You JSON extractor is working fine for me, check if you are using another post processor with the same variable name after this one.ararar
@ararar no, I don't use the same variable name in following post processors. Only in following requests.Alice M

2 Answers

0
votes

I have used your code and it is giving the correct results. Please check the below images. enter image description here

enter image description here I have tried with version 3.1 also and it is working fine.

Hope this helps.

Check the below image for different types of options in view result tree. enter image description here

0
votes

The $..[?(@.title == 'Money')].id expression should work just fine:

JMeter JSON Path Tester Conditional

Most probably your JMeter installation is corrupt and you experience some form of jar hell due to some clashing library in JMeter Classpath (it might be caused by presence of deprecated JSON Plugins or similar) . So I would recommend obtaining clean latest version of JMeter and trying out your test on it. If you're using any plugins - install them using JMeter Plugin Manager


If you are not in position to re-install JMeter you can try to get to the bottom of the issue by looking into jmeter.log file. If there are no suspicious entries - add the next line to log4j2.xml file:

<Logger name="org.apache.jmeter.extractor.json" level="debug" />