JSON Data Validation Levels and its Usage in Postman

Testing JSON could be a part of contract testing — you check whether the response body corresponds to specification (or contract or terms of reference).

JSON Data Validation Levels and its Usage in Postman
  1. JSON validation — low-level check;
  2. Required keys;
  3. Data types;
  4. Default values;
  5. Required values;
  6. Additional criteria — high-level check.

1. JSON validation

This is an initial check that answers the question: Is the body parsable?

2. Required keys

Does a JSON object contain required key/value pairs?

3. Data types

What data type should have the value of a given key? Number, string, boolean, array, object or null?

4. Default values

This point is rarely specified in documentation, but for some JSON keys you have to figure out what is considered the correct value.

{
name: "Marcus Aurelius",
school: "Stoicism"
}

5. Required values

It could be anything related to documentation: formats of the dates, numerical boundaries, etc.

{
name: "Marcus Aurelius",
school: "Stoicism",
occupation: ["philosopher", "emperor"]
}

6. Additional criteria

This level is strongly tied up to specification and can not be universal for equivalent requests.

{
name: "Marcus Aurelius",
school: "Stoicism",
reign: [161, 180]
}

Usage of validation levels

Each key/value pair in the response body could be checked both manually and automatically.

{
"lat": 40.1811,
"lon": 44.5136,
"timezone": "Asia/Yerevan",
"timezone_offset": 14400
}

1. How JSON data validation levels MAY look like in a script

If you take each validation level and and blindly impose it on the data structure, the test script may look like this:

pm.test("1. JSON validation", function () {
pm.response.to.be.json;
});

pm.test("2. Required keys", function () {
let jsonData = pm.response.json();

pm.expect(jsonData.lat).to.not.equal(undefined);
pm.expect(jsonData.lon).to.not.equal(undefined);
pm.expect(jsonData.timezone).to.not.equal(undefined);
pm.expect(jsonData.timezone_offset).to.not.equal(undefined);
});

pm.test("3. Data types", function () {
let jsonData = pm.response.json();

pm.expect(jsonData.lat).to.be.a('number');
pm.expect(jsonData.lon).to.be.a('number');
pm.expect(jsonData.timezone).to.be.a('string');
pm.expect(jsonData.timezone_offset).to.be.a('number');
});

pm.test("4. Default values", function () {
let jsonData = pm.response.json();

// timezone string should contain something
pm.expect(jsonData.timezone).to.have.lengthOf.above(0);
});

pm.test("5. Required values", function () {
let jsonData = pm.response.json();

pm.expect(jsonData.lat).to.be.within(-90, 90);
pm.expect(jsonData.lon).to.be.within(-180, 180);
});

pm.test("6. Аdditional criteria", function () {
let jsonData = pm.response.json();

pm.expect(jsonData.timezone).to.have.string('Yerevan');
});
How JSON data validation levels MAY look like in a script
Tests passed

2. How JSON data validation levels SHOULD look like in a script

A more correct way is an individual checks of each value:

let jsonData;

pm.test("Response body should be JSON", function () {
// 1. JSON validation
// Invalid JSON body will not be parsed
pm.response.to.be.json;
// Assigning the variable if JSON body is parsable
jsonData = pm.response.json();
});

pm.test("lat key should be correct", function () {
// 2-3. Required keys and data types
// If the key is missing, the test will fail
pm.expect(jsonData.lat).to.be.a('number');
// 4-5. Default and required values
pm.expect(jsonData.lat).to.be.within(-90, 90);
});

pm.test("lon key should be correct", function () {
pm.expect(jsonData.lon).to.be.a('number');
pm.expect(jsonData.lon).to.be.within(-180, 180);
});

pm.test("timezone key should be correct", function () {
pm.expect(jsonData.timezone).to.be.a('string');
// 6. Additional criteria
pm.expect(jsonData.timezone).to.have.string('Yerevan');
});

pm.test("timezone key should be correct", function () {
pm.expect(jsonData.timezone_offset).to.be.a('number');
});
How JSON data validation levels SHOULD look like in a script
Tests passed

3. JSON data validation through JSON Schema

JSON Schema allows to test all validation levels at once!

pm.test("Schema is valid", function () {
// 1. JSON validation
// Invalid JSON body will not be parsed
let jsonData = pm.response.json();

let schema = {
"type": "object",
"properties": {
"lat": {
// 3. Data types
"type": "number",
// 4-5. Default and required values
"minimum": -90,
"maximum": 90
},
"lon": {
"type": "number",
"minimum": -180,
"maximum": 180
},
"timezone": {
"type": "string",
// 6. Additional criteria
"pattern": "Yerevan"
},
"timezone_offset": {
"type": "number"
}
},
// 2. Required keys
"required": [
"lat",
"lon",
"timezone",
"timezone_offset"
]
};

pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
Test passed
Test failed

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrey Enin

Quality assurance engineer: I’m testing web applications, APIs and doing automation testing.