What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Despite the "JavaScript" in the name, JSON is completely language-independent — every major programming language ships with a built-in JSON library. JSON is defined by RFC 8259 and originally popularized by Douglas Crockford at json.org.
When you call a REST API, configure a Node.js project with package.json, store documents in MongoDB, or read a GitHub webhook payload, you are working with JSON. It is the universal data format of the modern web.
The Six JSON Data Types
JSON supports exactly six value types — no more, no less:
| Type | Example | Notes |
|---|---|---|
| String | "Hello, World!" | Must use double quotes — single quotes are invalid |
| Number | 42, 3.14, -7 | Integer or floating point, no NaN or Infinity |
| Boolean | true, false | Lowercase only |
| Null | null | Represents the absence of a value |
| Object | { "key": "value" } | Unordered key-value pairs; keys must be strings |
| Array | [1, "two", true] | Ordered list of any JSON values |
A complete JSON document showing all six types:
{
"name": "Ravi Mehta",
"age": 28,
"score": 98.5,
"verified": true,
"referral": null,
"tags": ["developer", "designer"],
"address": {
"city": "Surat",
"pincode": "395007"
}
}JSON Syntax Rules
JSON has strict syntax rules. Break any one of them and every parser throws an error:
| Rule | Valid | Invalid |
|---|---|---|
| Keys must be strings | "name": "Ravi" | name: "Ravi" |
| Strings use double quotes | "city": "Surat" | 'city': 'Surat' |
| No trailing comma | {"a": 1, "b": 2} | {"a": 1, "b": 2,} |
| No comments | — | // this is a comment |
| No undefined or NaN | "val": null | "val": undefined |
| Booleans are lowercase | true, false | True, False |
JSON vs XML — A Quick Comparison
Before JSON became dominant, XML was the standard. Here is the same data in both formats:
<!-- XML — verbose, requires closing tags -->
<user>
<name>Ravi Mehta</name>
<age>28</age>
<verified>true</verified>
</user>{ "name": "Ravi Mehta", "age": 28, "verified": true }JSON wins on brevity, native browser support, and direct mapping to programming language objects and arrays. XML is still used for SOAP APIs, RSS feeds, and document formats like Microsoft Office.
Parsing and Generating JSON in JavaScript
Every modern browser and Node.js runtime includes two global functions for working with JSON:
// JSON.parse — convert a JSON string into a JavaScript value
const jsonString = '{"name":"Ravi","age":28}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // => "Ravi"
// JSON.stringify — convert a JavaScript value into a JSON string
const data = { name: "Ravi", age: 28, score: 98.5 };
const str = JSON.stringify(data); // minified
const pretty = JSON.stringify(data, null, 2); // formatted with 2-space indent
// Always use try/catch — JSON.parse throws on invalid input
try {
const parsed = JSON.parse(rawInput);
} catch (e) {
console.error("Invalid JSON:", e.message);
}Key behaviors of JSON.stringify you should know:
- undefined values in objects are silently dropped (the key disappears)
- undefined values in arrays become null
- Functions, Symbols, and circular references cannot be serialized
- NaN and Infinity both become null
JSON in Other Languages
# Python
import json
obj = json.loads('{"name": "Ravi", "age": 28}')
print(obj["name"]) # => Ravi
back = json.dumps(obj, indent=2)// Go
import "encoding/json"
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
var u User
json.Unmarshal([]byte(`{"name":"Ravi","age":28}`), &u)Common Use Cases
- REST APIs — virtually every API today sends and receives JSON
- Configuration files — package.json, tsconfig.json, .eslintrc.json
- Database documents — MongoDB, Firestore, DynamoDB, and PostgreSQL JSONB columns all store JSON
- Browser storage — localStorage and sessionStorage via JSON.stringify/parse
- Message queues — Kafka, SQS, and RabbitMQ payloads are typically JSON
- Log aggregation — structured JSON logs are indexable and queryable in tools like Datadog and CloudWatch
Common Mistakes Beginners Make
- Trailing comma after the last property or array element —
{"a":1,}is invalid - Single quotes —
{'name':'Ravi'}is not JSON; use{"name":"Ravi"} - Unquoted keys —
{name:"Ravi"}is JavaScript object literal syntax, not JSON - Comments — JSON has no comment syntax; remove them before parsing
- undefined — not a JSON type; replace with
nullor omit the field
Use JSONKit's JSON Formatter to instantly catch all of these errors with line-and-column error messages.
Tools to Work with JSON
JSONKit provides a full set of free browser-based tools:
- JSON Formatter — format and beautify with 2/4-space indent
- JSON Minifier — compress for production API responses
- JSON Validator — validate syntax and JSON Schema
- JSON to CSV — export arrays to spreadsheets
- JSON to YAML — convert to config file format
- JSON Diff — compare two JSON objects
- JWT Decoder — decode and inspect JWT tokens
All tools run entirely in your browser. Your data is never sent to any server.
References
- RFC 8259 — The JSON Data Interchange Format — The official JSON specification by IETF
- json.org — The original JSON website by Douglas Crockford