JSONHint

Preparing your workspace…

Format JSON safely: a debugging workflow

A worked example with practical checks and limitations.

By JSONHint · Updated

A formatter changes presentation. It cannot establish what an author intended when a response is incomplete or a value is missing. Keep the original input, separate syntax repair from formatting and inspect the result before copying it into a project.

Start with a deliberately broken response

{"orderId":42,"paid":false,"items":["pen",]}

The trailing comma after the last item is invalid in strict JSON. Open the JSON error fixer and inspect its suggested repair. In this example the intended array contains one string, so removing the comma is a reasonable correction. A missing value after a colon is more ambiguous: a tool cannot know whether the intended value was null, a string or an object.

Format a known-valid document

{"orderId":42,"paid":false,"items":["pen"]}
  1. Paste the corrected document into the formatter.
  2. Choose indentation and format the document.
  3. Check that orderId remains a number, paid remains a Boolean and items remains an array.
  4. Copy or download the output only after inspecting the values.

Minifying the same document removes optional whitespace. It should not remove a space inside a string. Key sorting can help compare objects, but changes the serialized representation and therefore can change signatures or hashes.

Valid syntax can still hide a problem

JSON object member names should be unique. If a payload repeats a key, ordinary JavaScript parsing commonly retains the last value. Formatting the parsed result can hide that duplication. Large numeric identifiers can also lose precision beyond the safe integer range of JavaScript numbers. An identifier such as a long account number is usually safer as a string in the original contract.

An empty document is not valid JSON; null is. Single quotes, comments, NaN and undefined are not JSON values. JavaScript object literals and JSON look similar but follow different rules. Consult RFC 8259 when a producer and consumer disagree about accepted syntax.

Finish by testing the contract

Formatting does not prove a required field exists. Use the schema validator for supported structural checks, and test business rules in the application. If an API rejects the result, capture the status code and response message and reduce the payload to a minimal example. Do not repeatedly auto-fix the same file without checking what changed.

Found an error? Send a reproducible example or a source correction.