Jmeter JSON Path Assertion

11,223

Solution 1

For start following JSONPath Assertion will test your statusCode

$.statusCode

put 200 to Expected Value of JSONPath Assertion.

This one is for userName

$.responseData.userName

Easy, isn't it? See Parsing JSON guide for more useful examples and how-tos.

Solution 2

I found the JSR223 Assertion with script language javascript to be the easiest. at least if you have knowledge in java and javascript. And no need to add any plugins.

My working code in detail:

var json = JSON.parse(SampleResult.getResponseDataAsString());
if (json.statusCode != 200) {
    AssertionResult.setFailureMessage("" 
        + json.statusCode 
        + " " + json.statusMessage
        + " " + json.errorMessage);
    AssertionResult.setFailure(true);
}

Solution 3

I personally prefer to use BSF PostProcessor in coupling with Groovy language. Example of how to parse JSON with Groovy you can find here how to parse json using groovy

Share:
11,223
Sreenivas
Author by

Sreenivas

Diverse experience in Information Technology with emphasis on Software Quality Assurance & Automation Testing. Onsite transition to Touch Guide Solutions Stockholm as a Quality Analyst.Capable of working independently, lead or work within a team environment  Software Testing (Manual & Automated)  Selenium Web Driver(Xpath, Eclipse, Firebug, Datadriven Framework and TestNG)  Performance Testing (Jmeter & Stress Stimulus)  Coded UI Test(Visual Studio - Automation)  Requirement Analysis  Test Case Preparation & Execution  Defect Reporting

Updated on June 20, 2022

Comments

  • Sreenivas
    Sreenivas almost 2 years

    Below is my JSON response data, I need to do assertion using the below response. I tried in many ways to write JSON path & Expected Value. Always assertion fails. What I want is please help to write the path and expected value for the below data

    {
        "statusCode": 200,
        "statusMessage": "Success",
        "errorMessage": "",
        "responseData": {
            "id": 15,
            "userName": "[email protected]",
            "firstName": "tarento",
            "lastName": "test1",
            "phoneNumber": "1234567812",
            "email": "[email protected]",
            "password": "",
            "city": "",
            "agentList": [
                {
                    "id": 37,
                    "userName": "[email protected]",
                    "firstName": "Sanjay",
                    "lastName": "rahul",
                    "phoneNumber": "7411269480",
                    "email": "[email protected]",
                    "password": "",
                    "active": true
                },
                {
                    "id": 68,
                    "userName": "[email protected]",
                    "firstName": "jinesh",
                    "lastName": "sumedhan",
                    "phoneNumber": "9400993826",
                    "email": "[email protected]",
                    "password": "",
                    "active": true
                },
                {
                    "id": 108,
                    "userName": "[email protected]",
                    "firstName": "Rahul",
                    "lastName": "Antony",
                    "phoneNumber": "9994590241",
                    "email": "[email protected]",
                    "password": "",
                    "active": true
                },
                {
                    "id": 304,
                    "userName": "[email protected]",
                    "firstName": "Agent",
                    "lastName": "Agent",
                    "phoneNumber": "9025699716",
                    "email": "[email protected]",
                    "password": "",
                    "active": true
                }
            ],
            "roleName": "admin",
            "sessionKey": "435tnerLt9813942160478oDse46345635#1",
            "partner": {
                "id": 1,
                "name": "Tarento",
                "cityList": [
                    "bangalore",
                    "mumbai"
                ],
                "phone": "1234567812",
                "url": ""
            },
            "isActive": true,
            "isDeleted": false,
            "roleId": 1,
            "countryCode": "",
            "tags": [
                {
                    "tagId": 1,
                    "name": "all",
                    "description": "this is default tag of all driver."
                },
                {
                    "tagId": 2,
                    "name": "airport",
                    "description": ""
                },
                {
                    "tagId": 3,
                    "name": "street",
                    "description": "any text message"
                },
                {
                    "tagId": 255,
                    "name": "night",
                    "description": "night"
                }
            ]
        }
    }
    

    I received the following response

  • Sreenivas
    Sreenivas about 10 years
    Hi thank you for responding me,I'm new to Jmeter and im not that comfortable with Post Processor could you share step by step how to check the assertion for json data
  • olyv
    olyv about 10 years
    Hi, I will be happy to help you but can you please show what you have tried so far?
  • Sreenivas
    Sreenivas about 10 years
    Please check the below images,my test plan & and its response
  • Sreenivas
    Sreenivas about 10 years
    Please check the above images,my test plan & and its response
  • olyv
    olyv about 10 years
    Why do you edit my answer?? Are you sure you wanted to answer me but not @Dmitriy T?
  • Can
    Can about 10 years
    Reverted to the original post, why don't you update your question, @Sreenivas?
  • Sreenivas
    Sreenivas about 10 years
    @Dmitriy T - sorry for editing ur answer .@Can I have posted my test plan and response. I need correct JSON path and expected value based on the above JSON data
  • Dmitri T
    Dmitri T about 10 years
    Add your JSONPath assertion as a child of your HTTP Request and remove brackets around 200