JSON (JavaScript Object Notation)
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
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.
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().
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?
Enjoying these tutorials?