Developer tools

How do you format JSON and make sure the data still matches?

Keep an untouched API response, format a working copy, and verify keys, value types, array order, nulls, and sensitive fields before reusing the JSON.

By: 3sec Editorial 7 min read 1419 words

Define what “still matches” means before formatting

Formatted JSON should represent the same parsed data, but it will not be the same text. Newlines, spaces, indentation, and sometimes the written form of escaped characters or numbers can change, so a byte-for-byte comparison is the wrong acceptance test for a formatting job.

Start by preserving the exact response or configuration as a read-only baseline. Work on a separate copy and identify the values that would expose a real mistake: a request or event ID, the number of records in an array, the first and last stable item IDs, status values, timestamps, and fields whose types matter.

For example, imagine a webhook response with an eventId, a customer object, and an items array containing four products. Before formatting it, record that eventId is a string, items has four entries in a specific order, refunded is a Boolean, and notes is explicitly null. Those checks are more useful than asking whether the output has the same line breaks.

Keep secrets out of the working sample when possible. Access tokens, cookies, authorization headers, email addresses, and production customer records do not become safe merely because the formatter runs in a browser. Use an approved handling environment and replace sensitive values only in a copy.

Validate the working copy before you edit punctuation

Paste the working copy into the JSON Formatter and Validator and choose Validate Only first. The current component uses the browser’s JSON.parse operation. When parsing succeeds, it reports the output character count and the number of nested object keys; you can then format with two spaces, four spaces, or a tab, minify the result, and copy the separate read-only output.

When parsing fails, the tool leaves the output empty and displays the parser message. If the JavaScript runtime includes a character position, the page converts that position into a line and column. Treat that location as a search area, not as an instruction to delete the highlighted character.

Inspect punctuation in a deliberate order:

  • Check the comma before the reported property or array element. A missing separator is often detected only when the parser reaches the next token.
  • Check that property names and strings use straight double quotes. Smart quotes from a document, single-quoted JavaScript, and an unescaped quote inside a message are not valid JSON strings.
  • Pair every { with } and every [ with ]. A response copied from a truncated log may be incomplete rather than incorrectly closed.
  • Remove log prefixes, HTTP status lines, or Markdown markers only when you can prove they are outside the JSON payload.

Do not repeatedly remove characters until the result turns valid. The formatter does not repair malformed input and cannot infer a missing business value. Return to the original API response, the producer’s logs, or the service documentation when the intended structure is unclear.

Compare structure and types, not just property names

Once the copy parses, compare it with the baseline in two passes. The structural pass checks the top-level properties, nesting, array lengths, and array order. The value pass checks the small set of IDs, states, quantities, dates, and null conditions that control the downstream task.

Types deserve explicit attention because several different values look similar in a quick visual scan:

  • "false" is a string, while false is a Boolean.
  • "0042" preserves an identifier’s leading zeros, while 42 is a number.
  • null, "", [], {}, and an omitted property carry different information.
  • An array is ordered. Reordering its elements can matter even when all objects are still present.

A successful parse proves only that the input is JSON syntax. It does not prove that an API contract permits the properties, that a date uses the required timezone, that a status is valid, or that an identifier belongs to the intended account.

The key count shown by the local tool is a useful alarm, but it is not an equality test. Two objects can have the same number of keys while containing different names and values. Array elements are not counted as object keys, and an application can require fields that the sample never contained.

Watch for cases where parsing can hide a problem

Some inputs need review before any parse-and-serialize workflow. Duplicate property names are a common example. If an object contains two status properties, a JavaScript parser keeps the later value; formatting the parsed object can make the earlier occurrence disappear. Ask the producer to emit unique property names instead of accepting the last one silently.

Very large unquoted integers also require care. JSON numbers are parsed as JavaScript numbers in this tool, and integers beyond the safe precision range may no longer retain every digit. Database IDs, payment references, and distributed-system IDs should usually arrive as quoted strings when exact digits matter. Compare any long numeric identifier with the untouched baseline before copying the output.

Text representation can change without changing the intended value. Whitespace is the obvious case, but escaped Unicode and number notation may also be serialized differently. If another system verifies the exact bytes, a signature, or a content hash, do not format that signed payload at all. Obtain a separate human-readable copy and leave the authenticated bytes untouched.

These limits are why “valid JSON” and “unchanged evidence” are separate claims. The formatter is suitable for making a working copy readable, not for certifying forensic preservation or cryptographic integrity.

After you have verified a representative, non-sensitive sample, the JSON to TypeScript Interface generator can infer interface or type declarations from that one value. It handles nested objects, arrays, null, and mixed array types in the current component, but it cannot discover optional fields that are absent or prove that every future API response follows the same contract.

If the payload is explicitly supplied as Base64-encoded text, decode a copy with the Base64 Encoder and Decoder before validating the JSON. Base64 is an encoding, not encryption, validation, or a data-integrity check. Decoding an unknown string does not make its contents trusted.

Keep transformations in separate stages. Decode only when the transport format requires it, validate the resulting JSON, format a copy, and then compare the business-critical fields. Combining all of those actions without intermediate checks makes it difficult to tell where a value changed.

Verify the exact output in its real consumer

Before sending a payload or committing a configuration, validate the final copied output one more time. Confirm the recorded event ID, array count, boundary item IDs, value types, and null conditions. Search for secrets that should not leave the approved environment, and make sure any placeholders did not break quoting or punctuation.

Then use the smallest safe check offered by the system that consumes the JSON. A configuration may have a local validation command or preview. An API may provide a test environment or schema validator. A webhook sample may need to be replayed only in a dedicated sandbox. Formatting alone cannot substitute for those application rules.

If the output passes JSON validation but fails in the consumer, stop editing punctuation. Check required properties, allowed enum values, authentication, date formats, numeric ranges, and the consumer’s documented schema. That separates a syntax problem from a contract problem and protects the untouched baseline from accidental edits.

Frequently asked questions

Does formatting JSON change the data?

It intentionally changes whitespace and layout. For ordinary values the parsed structure should remain equivalent, but duplicate keys, very large numbers, and exact-byte workflows need special handling before parsing and serializing.

Why does the parser point after the actual mistake?

A missing comma, quote, or closing bracket may not become contradictory until the parser reads the next token. Inspect the reported location, the preceding separator, and the nearest opening structure.

Is the displayed key count proof that two JSON values match?

No. It is a quick structural clue. Equal key counts can hide different property names, values, array contents, and application requirements.

Can generated TypeScript types validate my API response?

No. The generator infers declarations from one sample. It does not enforce the live API contract, find fields missing from the sample, or replace runtime validation.

Can I format a signed JSON payload and keep the signature valid?

Do not assume so. Formatting changes bytes, and many signatures or hashes cover the exact byte sequence. Keep the signed payload untouched and create a separate copy for reading.