'JSON Assertion to check format of JSON in Jmeter

I am using Jmeter for the first time and have response data as follows:

{"types":[{"type":"tag","probability":"0.94"},{"type":"word","probability":"0.03"},{"type":"search","probability":"0.01"},{"type":"model","probability":"0.0"},{"type":"express","probability":"0.0"},{"type":"serial","probability":"0.0"}],"suggestions":[{"model":"AXT","dist":0.4286},{"model":"XL","dist":0.4},{"model":"03H","dist":0.4},{"model":"03","dist":0.4},{"model":"1435","dist":0.375}]}

I am trying to validate with regex if the JSON has specific format that to have type and probability under types and model and dist under suggestions. I used following but it does not validate

$.types[*].type


Solution 1:[1]

I don't think JSON Assertion is the good way to proceed, maybe it worth considering asking for a JSON Schema and validating the response using JSR223 Assertion and Groovy language?

If you don't have the schema you can generate it from your JSON using online tools like this one, it gives me something like:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "types": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            },
            "probability": {
              "type": "string"
            }
          },
          "required": [
            "type",
            "probability"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            },
            "probability": {
              "type": "string"
            }
          },
          "required": [
            "type",
            "probability"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            },
            "probability": {
              "type": "string"
            }
          },
          "required": [
            "type",
            "probability"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            },
            "probability": {
              "type": "string"
            }
          },
          "required": [
            "type",
            "probability"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            },
            "probability": {
              "type": "string"
            }
          },
          "required": [
            "type",
            "probability"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            },
            "probability": {
              "type": "string"
            }
          },
          "required": [
            "type",
            "probability"
          ]
        }
      ]
    },
    "suggestions": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "model": {
              "type": "string"
            },
            "dist": {
              "type": "number"
            }
          },
          "required": [
            "model",
            "dist"
          ]
        },
        {
          "type": "object",
          "properties": {
            "model": {
              "type": "string"
            },
            "dist": {
              "type": "number"
            }
          },
          "required": [
            "model",
            "dist"
          ]
        },
        {
          "type": "object",
          "properties": {
            "model": {
              "type": "string"
            },
            "dist": {
              "type": "number"
            }
          },
          "required": [
            "model",
            "dist"
          ]
        },
        {
          "type": "object",
          "properties": {
            "model": {
              "type": "string"
            },
            "dist": {
              "type": "number"
            }
          },
          "required": [
            "model",
            "dist"
          ]
        },
        {
          "type": "object",
          "properties": {
            "model": {
              "type": "string"
            },
            "dist": {
              "type": "number"
            }
          },
          "required": [
            "model",
            "dist"
          ]
        }
      ]
    }
  },
  "required": [
    "types",
    "suggestions"
  ]
}

The above schema has been auto-generated from your JSON file, you might need to amend it according to your requirements.

So you can:

  1. Save the schema to your hard drive next to the .jmx script

  2. Add the following libraries to JMeter Classpath

    • commons-digester-1.8.1.jar
    • commons-validator-1.6.jar
    • everit-json-schema-1.14.0.jar
    • handy-uri-templates-2.1.8.jar
    • joda-time-2.10.2.jar
    • json-20201115.jar
    • re2j-1.3.jar
  3. Add JSR223 Assertion as a child of the request which returns the above response

  4. Put the following code into "Script" area

    def schema = org.everit.json.schema.loader.SchemaLoader.load(new org.json.JSONObject(new File('schema.json')))
    try {
        schema.validate(new org.json.JSONObject(prev.getResponseDataAsString()))
    }
    catch (Exception ex) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage(ex.getMessage())
    }
    
  5. If the response will not match schema - the assertion will fail the sampler.

More information:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T