How to use Postman to compare a response to a json file

Hello I’m new to using Postman and don’t have a programming background. I’m looking for assistance on how to use Postman to run an API endpoint and take that response and compare it to an external Json file that has the expected response. Any help is much appreciated! @danny-dainton

Hey @abbymille :waving_hand:

Welcome to the Postman Community! :postman:

You’re unable to access the local file system on your machine, from the Postman Client, so you couldn’t directly compare a response from the API and do a diff against a file. If you were to store the JSON in a variable or maybe a datafile for the Collection Runner, it could be done.

You’d be able compare against an external JSON file using a Postman Collection, Newman (Command Line Collection Runner) and a small node script but you wouldn’t be able to do all that inside of the platform. :frowning:

I can share a script with you but I’m not sure if that’s just going to be a too much if you’re quite new to Postman, more than happy to walk you through it.

Thank you for the speedy response Danny! Is there a limit to size in the variable? It is a lengthy file. If you wouldn’t mind to share a script with me I’ll see if I can figure it out. Thanks again!

I added my response in a variable and have this test but it doesn’t like it. Could you please advise what the issue may be? It doesn’t seem to be recognizing my variable as it isn’t in the dropdown list I have to fully type it. Thanks much!

WIthin the scripting sandbox environment you would use the following syntax to reference a variable:

pm.variables.get('TestFileCompare')

You could also narrow the scope of this but i don’t know where you have placed the data (collection/global/environment)

Away from the scripting sandbox you would using the {{TestFileCompare}} syntax to reference it.

You’d need to create a mini project (npm init) and somewhere to install the dependencies (newman, fs, diff).

const newman = require("newman");
const fs = require("fs");
const { diffJson } = require("diff");

// CONFIG: Update these paths
const COLLECTION_PATH = "./test_file_comparison.json"; // Path to your Postman collection
const EXPECTED_JSON_PATH = "./expected_response.json";
const TARGET_REQUEST_NAME = "File Compare Request";

let actualResponseBody = "";

// Run the collection
newman.run(
  {
    collection: require(COLLECTION_PATH),
    reporters: "cli",
    suppressExitCode: true,
    insecure: true,
  },
  function (err, summary) {
    if (err) {
      throw err;
    }

    const executions = summary.run.executions;
    const targetExecution = executions.find(
      (exec) => exec.item.name === TARGET_REQUEST_NAME
    );

    if (!targetExecution || !targetExecution.response) {
      console.error(
        `Request "${TARGET_REQUEST_NAME}" not found or has no response.`
      );
      return;
    }

    // Capture the actual response body
    actualResponseBody = targetExecution.response.stream.toString();

    // Read the expected response from file
    const expected = JSON.parse(fs.readFileSync(EXPECTED_JSON_PATH, "utf8"));

    // Parse actual response JSON
    let actual;
    try {
      actual = JSON.parse(actualResponseBody);
    } catch (e) {
      console.error("Actual response is not valid JSON:", e.message);
      return;
    }

    // Compare the actual response to the expected one
    const differences = diffJson(expected, actual);

    console.log("\n=== Diff between Expected and Actual ===\n");

    differences.forEach((part) => {
      const color = part.added
        ? "\x1b[32m"
        : part.removed
        ? "\x1b[31m"
        : "\x1b[0m";
      try {
        const parsed = JSON.parse(part.value);
        const pretty = JSON.stringify(parsed, null, 2);
        console.log(color + pretty + "\x1b[0m");
      } catch {
        // If it’s not JSON (e.g., plain text), just print as-is
        console.log(color + part.value + "\x1b[0m");
      }
    });
  }
);

That would output the diff to the console like this: