DEV Community

Cover image for How to Format and Validate JSON Easily
Kunal Naskar
Kunal Naskar

Posted on

How to Format and Validate JSON Easily

If you work with APIs, you've probably received a JSON response that looks like this:

{"name":"Kunal","role":"developer","skills":["React Native","JavaScript","TypeScript"],"experience":{"mobile":6,"web":4}}

It works, but it's not very easy to read.

Formatting it makes the structure much clearer:

{
"name": "Kunal",
"role": "developer",
"skills": [
"React Native",
"JavaScript",
"TypeScript"
],
"experience": {
"mobile": 6,
"web": 4
}
}

Why format JSON?

Formatted JSON makes it easier to:

  • Read API responses
  • Find specific properties
  • Debug API issues
  • Compare objects
  • Understand nested data
  • Identify syntax problems

This becomes especially useful when you're working with large API responses.

Formatting vs validating JSON

These are two different things.

Formatting makes valid JSON easier to read.

Validation checks whether the JSON follows the correct syntax.

For example, this is invalid:

{
"name": "Kunal",
"role": "developer",
}

There is an extra comma after "developer".

A JSON validator can help identify this type of syntax error.

Common JSON mistakes

  1. Using single quotes

This is not valid JSON:

{'name': 'Kunal'}

JSON requires double quotes:

{"name": "Kunal"}

  1. Trailing commas

This is invalid:

{
"name": "Kunal",
"role": "developer",
}

Remove the final comma:

{
"name": "Kunal",
"role": "developer"
}

  1. Incorrect brackets

Objects use {} and arrays use [].

{
"skills": [
"React Native",
"JavaScript"
]
}

Keeping the brackets correctly matched is important when working with deeply nested JSON.

Format JSON directly in your browser

If you just need a quick way to format and validate JSON, I built a small browser-based JSON Formatter as part of ShadowReference.

You can paste your JSON, format it and work with the result directly in the browser.

JSON Formatter: https://www.shadowreference.com/tools/json-formatter

No installation is required.

Conclusion
JSON formatting is a small thing, but it can save a lot of time when debugging APIs or working with large responses.

The next time you get a huge one-line JSON response, formatting it first can make the rest of the debugging process much easier.

Top comments (0)