Small SEO Tools

How to Compare JSON with Reordered Object Keys

Two JSON responses can look completely different when their object keys arrive in a different order. A plain text comparison may then bury a changed quantity among lines that merely moved. For a small review sample, sort the object keys in both working copies, use identical indentation, and compare the resulting text.

This walkthrough combines the JSON Formatter and Validator with the Text Compare tool. Keep the untouched responses separately: parsing and rebuilding JSON can discard information.

Separate object order from array order

RFC 8259 defines JSON objects without a required member order, while arrays have an element order. Moving status above order in an object need not change the represented data. Reversing a list of processing steps can change its meaning.

The fictional responses below describe the same product, but the second increases its quantity. Their object keys also appear in different positions.

Original response

{"order":{"quantity":2,"sku":"MUG-7"},"status":"ready","steps":["pack","ship"]}

Changed response

{"steps":["pack","ship"],"status":"ready","order":{"sku":"MUG-7","quantity":3}}

The question is whether the quantity changed, not whether the server serialized its keys in the same sequence. Use these invented records rather than a response containing private account data.

Prepare both copies with the same formatter settings

  1. Paste the original sample into Paste your JSON.
  2. Select 2 spaces for Indent, leave Minify unchecked, and check Sort keys alphabetically.
  3. Click Format JSON. Copy the output into a separate working document labelled Original.
  4. Replace the input with the changed sample. Keep all three settings the same, click Format JSON again, and save its output as Changed.

The original becomes:

{
  "order": {
    "quantity": 2,
    "sku": "MUG-7"
  },
  "status": "ready",
  "steps": [
    "pack",
    "ship"
  ]
}

The changed copy has this same layout with "quantity": 3. The sorting option also visits nested objects. It retains array element positions, so pack still precedes ship.

The formatter uses JSON.stringify to produce the display text; MDN documents its indentation parameter. Consistent spacing places corresponding fields on comparable lines. It does not make this tool a canonical serializer for signatures or hashes.

Compare the quantity without hiding meaningful differences

  1. Open Text Compare. Paste the prepared Original and Changed copies into their matching boxes.
  2. Uncheck Ignore case and Ignore leading and trailing whitespace. Leave Hide unchanged lines unchecked initially.
  3. Click Compare Texts. With the examples above and no added blank lines, expect 1 line added, 1 line removed and 10 lines unchanged.

The changed field appears as:

-     "quantity": 2,
+     "quantity": 3,

This is one changed value displayed as a removed line and an added line. Check the surrounding order object before recording the field as order.quantity; the tool shows text lines, not JSON paths.

Case matters in keys and string values: "ready" and "READY" are different strings. Keep string spacing intact too. The whitespace option trims line edges rather than spaces inside quoted strings, but identical formatter settings make that option unnecessary here.

As a control, change the second quantity back to 2 and format it again: the two prepared samples should match. Then reverse its steps to ["ship","pack"]. Those array positions must remain different after formatting; do not sort array values to silence the diff.

Reject a comparison that lost source information

Inspect the originals for duplicate names within an object. Parsing can retain only one occurrence, so equal rebuilt text cannot prove the original responses were equivalent. RFC 8259 recommends unique names and describes inconsistent receiver behavior for duplicates.

Large numbers can also lose precision during JSON.parse, as MDN explains. Follow the large integer ID guide when digits matter. String IDs are appropriate only when the schema permits them; do not change field types to conceal a parsing problem.

This sorting implementation is also unsuitable for preserving a member named __proto__. Use a purpose-built, precision-aware JSON comparison workflow for such data. For the small samples here, verify the retained keys, values, types and array order against the originals before accepting the quantity change. Preserve the raw responses whenever exact bytes, escape spelling or signatures matter.