Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Timers JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS JSON JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Browser API JS Web API JS Graphics

Old Technologies

JS AJAX JS jQuery JS JSONP

JS Examples

JS Examples

JavaScript JSON Syntax

JSON has a very simple syntax for representing structured data.

One JSON document consists of one JSON value.

The value can be:

  • An Object
  • An Array
  • A String
  • A Number
  • A Boolean
  • or null

JSON Object Literals

This is a JSON string:

'{"name":"John", "age":30, "car":null}'

Inside the JSON string there is a JSON object literal:

{"name":"John", "age":30, "car":null}

A JSON object is written between curly braces { }.

An object contains zero or more properties.

Properties comes in key/value pairs separated by commas.

key names are strings enclosed in double quotes.

Keys and values are separated by a colon.

JSON Object

Values must be a valid JSON data type:

  • string
  • number
  • object
  • array
  • boolean
  • null

It is a common mistake to call a JSON object literal "a JSON object".

JSON cannot be an object. JSON is a string format.

The data is only JSON when it is in a string format.

When a JSON object litteral is converted into a JavaScript variable, it becomes a JavaScript object.


JSON Array Literals

This is a JSON string:

'["Ford", "BMW", "Fiat"]'

Inside the JSON string there is a JSON array literal:

["Ford", "BMW", "Fiat"]

Other Examples

[
  "Apple",
  "Banana",
  "Orange"
]
[1, 2, 3, 4 ,5]

A JSON array is written between square brackets [ ].

An array contains zero or more values.

Values are separated by commas.

JSON Object

Values must be a valid JSON data type:

  • string
  • number
  • object
  • array
  • boolean
  • null

JSON Values

JSON supports six value types.

Value Example
String "John"
Number 42
Boolean true
Null null
Object {"name" : "John"}
Array [1, 2, 3]


JSON syntax is derived from JavaScript object notation syntax:

  • Curly braces hold objects
  • Properties comes is in name/value pairs
  • Properties are separated by commas
  • Square brackets hold arrays
  • Strings are in double quotes

But, JSON has stricter syntax rules than JavaScript objects.


JSON Does Not Allow

JSON has a small, strict syntax.

The following JavaScript features are not valid JSON:

Feature JSON JS
Unquoted property names No Yes
Single quoted strings No Yes
Trailing commas No Yes
Comments No Yes

JSON Property Names

Every property name must be a string.

Property names must be enclosed in double quotes.

Valid JSON

{
  "name": "John"
}

Invalid JSON

{
  name: "John"
}

JSON Strings

JSON strings must use double quotes.

Valid JSON

{
  "city": "London"
}

Invalid JSON

{
  "city": 'London'
}

Whitespace

Whitespace is ignored outside strings.

You can use spaces, tabs, and line breaks to make JSON easier to read.

Example

{"name":"John","age":30}

Equivalent JSON

{
  "name": "John",
  "age": 30
}

Trailing Commas

JSON does not allow a comma after the last property or array value.

Wrong

{
  "name": "John",
  "age": 30,
}

Correct

{
  "name": "John",
  "age": 30
}

Comments Are Not Allowed

JSON does not support comments.

Wrong

{
  // Customer name
  "name": "John"
}


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->