📊

JSON Validation and Postman Testing

Mar 14, 2025

Class Notes: JSON Validation and Postman Testing

Introduction to JSON and JSON Path

  • JSON: JavaScript Object Notation
  • Used for structuring data
  • JSON Path: Used to identify nodes in JSON to locate data

Key Components of API Response Validation

  1. Status Code
  2. Headers
  3. Cookies
  4. Response Time
  5. Response Body

Importance of Knowing Response Structure

  • Understand the expected type of response before sending requests.
  • Analyze response structure to determine validation points.
  • Run the API at least once to familiarize with response.

Validation Points in Postman

  • Validation Points: Assertions to validate the response
  • Use Postman's pm library for assertions
  • Assertions can be added using JavaScript functions

Types of Functions in Postman

  1. Normal Functions

    function myFunction() { // function body }
  2. Arrow Functions

    const myFunction = () => { // function body }

Validating the Response

  • Use pm.test() to write assertions
  • Example for checking status code: pm.test("Status code is 200", function () { pm.response.to.have.status(200); });

Validating Headers

  • Validate presence and value of headers: pm.test("Content-Type header is present", function () { pm.response.to.have.header("Content-Type"); }); pm.test("Content-Type is application/json", function () { pm.response.to.have.header("Content-Type", "application/json"); });

Validating Cookies

  • Validate presence and value of cookies: pm.test("Language cookie is present", function () { pm.cookies.has("language"); }); pm.test("Language cookie value is en-GB", function () { pm.expect(pm.cookies.get("language")).to.equal("en-GB"); });

Validating Response Time

  • Check if response time is below a set threshold: pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); });

Validating Response Body

  • Capture the entire response body: const jsonData = pm.response.json();

Asserting Value Types

  • Validate the type of each value in the response: pm.test("ID is a number", function () { pm.expect(jsonData.id).to.be.a('number'); }); pm.test("Name is a string", function () { pm.expect(jsonData.name).to.be.a('string'); });

Asserting Array Properties

  • Validate contents of arrays: pm.expect(jsonData.courses).to.include('Java'); pm.expect(jsonData.courses).to.have.members(['Java', 'Selenium']);

Validating JSON Fields in Response

  • Validate specific fields and their values: pm.expect(jsonData.name).to.equal("John"); pm.expect(jsonData.location).to.equal("India");

JSON Schema Validation

  • Schema describes the structure of the JSON response:
    • Can be generated using tools
    • Use TV4 library for validation:
    pm.test("Schema is valid", function () { pm.expect(tv4.validate(jsonData, schema)).to.be.true; });

Conclusion

  • Validate each component based on response type.
  • Use the same validation process for all types of APIs.
  • Important for GET requests to focus on response body and schema validation.
  • Upcoming classes will cover additional topics.