On this page
- 1 JavaScript: JSON (JavaScript Object Notation)
- 2 JavaScript: JSON (JavaScript Object Notation)
- 2.1 What is JSON?
- 2.2 2 things to know abut JSON
- 2.3 Example
- 2.4 Explanation
- 2.5 JSON: Data types
- 2.6 Difference between JSON text and the javascript object
- 2.6.0.1 Introduction to the DOM
- 2.6.0.2 How to apply CSS to iframe?
- 2.6.0.3 JavaScript Map, Reduce, and Filter
- 2.6.0.4 JavaScript Concepts Everyone Should Learn!
- 2.6.0.5 JavaScript: localStorage and sessionStorage
- 2.6.0.6 JavaScript: Array functions/methods
- 2.6.0.7 JavaScript: Array map() Method
- 2.6.0.8 Promise in JavaScript with example
- 2.6.0.9 JavaScript: FormData
- 2.6.0.10 JavaScript: How ES6 Arrow Function Can Help You
- 2.6.0.11 ES6: Spread operator
- 2.6.0.12 ES6: Spread syntax
- 2.6.0.13 ES6: Introduction
- 2.6.0.14 JavaScript: Scope and the Variable this
- 2.6.0.15 JavaScript: Get Element
- 2.6.0.16 JavaScript: Application programming interface (API)
- 2.6.0.17 Javascript : Date & Time Program
- 2.6.0.18 JavaScript: What is AJAX?
- 2.6.0.19 JavaScript: Getting started
JavaScript: JSON (JavaScript Object Notation)
JavaScript: JSON (JavaScript Object Notation)
In this post, we’ll talk about JSON ( JavaScript Object Notation).
On this page
What is JSON?
JavaScript Object Notation (JSON) is an open-standard file format or data interchange format that uses human-readable text to transmit data objects consisting of attribute-value pairs and array data types (or any other serializable value). It is a very common data format, with a diverse range of applications, such as serving as a replacement for XML in AJAX systems. (Wikipedia)
2 things to know abut JSON
> Keys - a string wrapped in "" > Values - can be a string, number, boolean expression, array, or object
{ "name": "John", age: 16, "ocean": { "chosen": true, "voyaged": false } }
console.log(json.name); //John console.log(json.age); //16
Example
var data = { "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ] }
Explanation
{employees: Array(3)}
This example defines an employees object: an array of 3 employee records (objects)
data.employees[0].firstName "John"
JSON: Data types
string number object (JSON object) array boolean null
Difference between JSON text and the javascript object
Javascript Object is a variable type and JSON is a string(text).
JSON.parse() - turn a string into JSON JSON.stringify() - turn JSON into a string
JSON.parse() – Convert text into a JavaScript object (turn a string into JSON)
JSON.stringify() – convert JavaScript object into a string (turn JSON into a string)