Search code examples
jsongroovyjmeterjsr223

How can I verify a nested JSON response using groovy in Jmeter?


IAm testing my restful API through Jmeter. I get Json that includes the same many keys like ("id",MI,DS etc ) multiple times. I am unable to verify through following code :

String expected="Iam writing following complete JSON Response as it is"
JSONObject data = getRESTData({vars.get("APIResponsedata")});
    JSONAssert.assertEquals(expected, data, false);

-where APIResponsedata is Json response iam getting through regular expression extractor

Response object:

{
    "success": true,
    "error": null,
    "data": {
        "Months": [
            {
                "Id": 0,
                "MDN": "January 2020",
                "H": [
                    {
                        "MI": 100,
                        "MN": "API Automation Testing Lower the better Metric-Sum",
                        "GL": "B",
                        "DS": "%",
                        "RT": "s"
                    },
                    {
                        "MI": 101,
                        "MN": "API Automation Testing Higher the better Metric-Sum",
                        "GL": "A",
                        "DS": "%",
                        "RT": "s"
                    },
                    {
                        "MI": 102,
                        "MN": "API Automation Testing Higher the better Metric-AVG",
                        "GL": "A",
                        "DS": "s",
                        "RT": "a"
                    },
                    {
                        "MI": 103,
                        "MN": "API Automation Testing Lower the better Metric-Number-AVG",
                        "GL": "B",
                        "DS": "s",
                        "RT": "a"
                    }
                ],
                "T": [
                    {
                        "MI": 100,
                        "VL": "0.20",
                        "NA": false,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 101,
                        "VL": "0.20",
                        "NA": false,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 102,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 103,
                        "VL": "0.20",
                        "NA": false,
                        "PTG": 0,
                        "S": null
                    }
                ],
                "MR": [
                    {
                        "MI": 100,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 101,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 102,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    },
                    {
                        "MI": 103,
                        "VL": "0.00",
                        "NA": true,
                        "PTG": 0,
                        "S": null
                    }
                ],
                "WR": [
                    {
                        "WN": 1,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "29/12/2019",
                        "ED": "04/01/2020"
                    },
                    {
                        "WN": 2,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "05/01/2020",
                        "ED": "11/01/2020"
                    },
                    {
                        "WN": 3,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "12/01/2020",
                        "ED": "18/01/2020"
                    },
                    {
                        "WN": 4,
                        "DT": [
                            {
                                "MI": 100,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 101,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 102,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            },
                            {
                                "MI": 103,
                                "VL": "0.00",
                                "NA": true,
                                "PTG": 0,
                                "S": null
                            }
                        ],
                        "SD": "19/01/2020",
                        "ED": "25/01/2020"
                    }
                ],
                "SD": "01/01/2020",
                "ED": "31/01/2020"
            }
        ],
        "MND": "2019-12-29T00:00:00Z",
        "MXD": "2020-01-25T00:00:00Z"
    }
}

IAm getting following error:

 Assertion error: true
    Assertion failure: false
    Assertion failure message: javax.script.ScriptException: 
     org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
    failed:
    Script50.groovy: 5: expecting anything but ''\n''; got it anyway @ line 5, column 18.
       "{"success":true,
                        ^

Solution

  • Iam writing following complete JSON Response as it is

    this is your problem, you either need to use single quotation marks like:

    String expected=`Iam writing following complete JSON Response as it is`
    

    or escape all double quotation marks with a backslash like:

    String expected = "{\n" +
            "    \"success\": true,\n" +
    

    or for better readability use triple-single-quoted-string like:

    String expected = '''
    {
      "success": true,
      "error": null,
      "data": {
        "Months": [
          {
            "Id": 0,
            "MDN": "January 2020",
            "H": [
              {
                "MI": 100,
                "MN": "API Automation Testing Lower the better Metric-Sum",
                "GL": "B",
                "DS": "%",
                "RT": "s"
              },
              {
                "MI": 101,
                "MN": "API Automation Testing Higher the better Metric-Sum",
                "GL": "A",
                "DS": "%",
                "RT": "s"
              },
              {
                "MI": 102,
                "MN": "API Automation Testing Higher the better Metric-AVG",
                "GL": "A",
                "DS": "s",
                "RT": "a"
              },
              {
                "MI": 103,
                "MN": "API Automation Testing Lower the better Metric-Number-AVG",
                "GL": "B",
                "DS": "s",
                "RT": "a"
              }
            ],
            "T": [
              {
                "MI": 100,
                "VL": "0.20",
                "NA": false,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 101,
                "VL": "0.20",
                "NA": false,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 102,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 103,
                "VL": "0.20",
                "NA": false,
                "PTG": 0,
                "S": null
              }
            ],
            "MR": [
              {
                "MI": 100,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 101,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 102,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              },
              {
                "MI": 103,
                "VL": "0.00",
                "NA": true,
                "PTG": 0,
                "S": null
              }
            ],
            "WR": [
              {
                "WN": 1,
                "DT": [
                  {
                    "MI": 100,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 101,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 102,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 103,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  }
                ],
                "SD": "29/12/2019",
                "ED": "04/01/2020"
              },
              {
                "WN": 2,
                "DT": [
                  {
                    "MI": 100,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 101,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 102,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 103,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  }
                ],
                "SD": "05/01/2020",
                "ED": "11/01/2020"
              },
              {
                "WN": 3,
                "DT": [
                  {
                    "MI": 100,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 101,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 102,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 103,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  }
                ],
                "SD": "12/01/2020",
                "ED": "18/01/2020"
              },
              {
                "WN": 4,
                "DT": [
                  {
                    "MI": 100,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 101,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 102,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  },
                  {
                    "MI": 103,
                    "VL": "0.00",
                    "NA": true,
                    "PTG": 0,
                    "S": null
                  }
                ],
                "SD": "19/01/2020",
                "ED": "25/01/2020"
              }
            ],
            "SD": "01/01/2020",
            "ED": "31/01/2020"
          }
        ],
        "MND": "2019-12-29T00:00:00Z",
        "MXD": "2020-01-25T00:00:00Z"
      }
    }
    '''
    

    More information: