DEV Community

TheAppsFirm
TheAppsFirm

Posted on

The Complete Guide to JSON: Format, Validate, and Debug Like a Pro

If you work with APIs, you work with JSON. And if you have ever spent 20 minutes hunting for a missing comma in a 500-line JSON response, this guide is for you.

What is JSON?

JSON (JavaScript Object Notation) is the de facto data interchange format for web APIs. It is human-readable, lightweight, and supported by every programming language.

{
  "name": "The Apps Firm",
  "tools": 48,
  "free": true,
  "categories": ["formatters", "generators", "calculators"]
}
Enter fullscreen mode Exit fullscreen mode

Common JSON Mistakes

1. Trailing commas

JSON does not allow trailing commas. JavaScript does, which causes confusion when copying objects between JS and JSON.

2. Single quotes

JSON requires double quotes for all strings and keys. Single quotes are invalid.

3. Unquoted keys

Unlike JavaScript objects, JSON keys must always be quoted strings.

4. Comments

JSON does not support comments. Use JSONC or JSON5 if you need them.

How to Format JSON

Command line:

python3 -m json.tool data.json
curl -s https://api.example.com/data | jq .
Enter fullscreen mode Exit fullscreen mode

In your editor:

  • VS Code: Select JSON, press Shift+Alt+F
  • Vim: :%!python3 -m json.tool

Online:
I built a free JSON formatter that does formatting, validation, minification, tree view, and JSON-to-YAML/CSV conversion entirely in your browser.

How to Validate JSON

function isValidJSON(str) {
  try { JSON.parse(str); return true; }
  catch (e) { return false; }
}
Enter fullscreen mode Exit fullscreen mode

JSON vs YAML vs XML

Feature JSON YAML XML
Readability Good Best Worst
Comments No Yes Yes
File size Small Smallest Largest
Parsing speed Fast Slow Slow
Use case APIs, config Config, K8s Legacy, SOAP

Useful JSON Tools

Pro Tips

  1. Always validate API responses before parsing
  2. Use JSON.stringify(obj, null, 2) for readable logging
  3. Handle BigInt - JSON.parse cannot handle numbers larger than 2^53
  4. Use streaming parsers for large files
  5. Sanitize before parsing to prevent prototype pollution

What JSON tools do you use daily? Share in the comments.

Top comments (0)