Web Analytics

JSON (JavaScript Object Notation)

Practical ~15 min read

What is JSON?

JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format.

  • JSON is text, written with JavaScript object notation.
  • JSON is language independent.
  • JSON is easy for humans to read and write.

JSON Syntax

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays
Important: JSON keys must be strings, written with double quotes.

JSON.parse()

A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.

HTML
CSS
JS

JSON.stringify()

When sending data to a web server, the data has to be a string. Convert a JavaScript object into a string with JSON.stringify().

HTML
CSS
JS

Summary

  • JSON is a text format for storing and transporting data.
  • Use JSON.parse() to convert a JSON string to a JavaScript object.
  • Use JSON.stringify() to convert a JavaScript object to a JSON string.

Quick Quiz

Which method converts a JavaScript object to a JSON string?

A
JSON.parse()
B
JSON.toString()
C
JSON.stringify()
D
JSON.convert()