What is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for storing and exchanging structured data. It writes data as key/value pairs inside curly braces and lists inside square brackets, so people can read it and every programming language can parse it.

{
  "name": "Ada",
  "age": 36,
  "skills": ["math", "poetry"]
}

Try it: build a JSON object

Change a key, a type or a value and watch the JSON and the tree update. Notice which values get quotes and which do not.

Your JSON

{
  "name": "Ada Lovelace",
  "age": 36,
  "isAdmin": true,
  "skills": [
    "math",
    "poetry",
    1843
  ],
  "address": {
    "city": "London",
    "zip": "W1"
  },
  "nickname": null
}

Tree view: what a program sees

  • object6 keys
    • namestring"Ada Lovelace"
    • agenumber36
    • isAdminbooleantrue
    • skillsarray3 items
      • 0string"math"
      • 1string"poetry"
      • 2number1843
    • addressobject2 keys
      • citystring"London"
      • zipstring"W1"
    • nicknamenullnull

JSON syntax and the 6 data types

Data is written as "key": value pairs separated by commas. Objects use curly braces, arrays use square brackets, and every value is one of these six types.

TypeExampleRule
String"Ada"Text in double quotes
Number36, -2.5, 1e3No quotes; no NaN or Infinity
Booleantrue, falseLowercase, no quotes
NullnullAn empty or unknown value
Array["math", 1843]An ordered list in square brackets
Object{ "city": "London" }Key/value pairs in curly braces

Spot the JSON syntax error

Most invalid JSON comes from six mistakes. Pick one to see the error your browser reports and the fix.

Broken

{ 'name': 'Ada' }

Your browser's JSON.parse says: …

Fixed

{ "name": "Ada" }

JSON strings and keys must use double quotes. Single quotes are fine in JavaScript, not in JSON.

Got a file that will not parse? Paste it into the JSON formatter and validator to find the exact line.

Where JSON is used

Web APIs
When an app loads your feed or weather, the server almost always answers with JSON.
Config files
package.json, tsconfig.json and VS Code's settings.json all store settings as JSON.
Databases
MongoDB stores JSON-like documents, and PostgreSQL and MySQL have JSON column types.
Saving app data
Browsers store app state in localStorage as JSON strings, and many games save progress the same way.

JSON vs XML vs YAML

Here is the same person written in all three formats.

JSON

{
  "name": "Ada",
  "skills": ["math", "poetry"]
}

XML

<person>
  <name>Ada</name>
  <skills>
    <skill>math</skill>
    <skill>poetry</skill>
  </skills>
</person>

YAML

name: Ada
skills:
  - math
  - poetry
FeatureJSONXMLYAML
Best forWeb APIs and data exchangeDocuments, older enterprise and SOAP systemsConfig files people edit by hand
ReadabilityGoodWordy: every value needs an opening and closing tagBest: no brackets or quotes needed
CommentsNoYesYes
Data typesString, number, boolean, null, array, objectEverything is text unless a schema says otherwiseSame as JSON, plus dates and more
Parsing in JavaScriptBuilt in: JSON.parseNeeds DOMParser or a libraryNeeds a library

How to write valid JSON

  1. Start with curly braces. Wrap the whole object in { and }. Use [ and ] instead if the top level is a list.
  2. Write each key in double quotes. Type the key as a string, a colon, then the value: "name": "Ada".
  3. Pick the right value type. Quote strings, leave numbers, true, false and null unquoted, and nest arrays or objects as needed.
  4. Separate pairs with commas. Put a comma between items, but never after the last one.
  5. Validate it. Paste it into a JSON validator, or run JSON.parse, to catch a stray quote or comma before you ship it.

JSON quiz: test yourself

  1. 1. What does JSON stand for?
  2. 2. Which line is valid JSON?
  3. 3. Which of these is not a JSON data type?
  4. 4. Can you put comments in a JSON file?
  5. 5. Which JavaScript function turns a JSON string into an object?

0 of 5 answered

Frequently asked questions

What is JSON in simple terms?

JSON (JavaScript Object Notation) is a lightweight text format for storing and sending structured data. It writes data as key/value pairs in curly braces and lists in square brackets, so both people and programs can read it.

What is JSON used for?

JSON is mostly used to send data between a server and a web or mobile app through APIs. It is also used for config files such as package.json, for storing documents in databases, and for saving app data.

Is JSON a programming language?

No. JSON is a data format, not a language. It cannot run code, loop or make decisions. It only describes data, which a program in any language can read and write.

What is the difference between JSON and a JavaScript object?

A JavaScript object is live data in memory and can hold functions, undefined and single-quoted strings. JSON is text with stricter rules: double-quoted keys and strings, no functions, no comments. JSON.stringify and JSON.parse convert between them.

Is JSON better than XML?

For web APIs, usually yes: JSON is shorter, faster to parse and maps straight onto objects in code. XML is still used for documents, older enterprise systems and formats that need attributes, namespaces or comments.

How do I open a JSON file?

A .json file is plain text, so any text editor opens it, including Notepad, TextEdit or VS Code. Browsers like Firefox and Chrome also display JSON, and a JSON formatter makes long files easier to read.