JavaScript JSON JAVASCRIPT
- JavaScript Introduction
- JavaScript Syntax
- JavaScript innerHTML
- JavaScript document.write()
- Javascript - window.alert()
- JavaScript - console.log()
- JavaScript Comments
- JavaScript Variables
- JavaScript Operators
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Conditions
- JavaScript Switch
- JavaScript Loop For
- JavaScript While Loop
- JavaScript Break and Continue
- JavaScript Type Conversion
- JavaScript Errors
- JavaScript Scope
- JavaScript this Keyword
- JavaScript Classes
- JavaScript Debugging
- JavaScript - Changing CSS
- JavaScript JSON
JavaScript JSON
What is JSON?
JSON is a format for storing and transporting data. JSON is often used when data is sent from a server to a web page.
- JSON stands for JavaScript Object Notation
- JSON is a lightweight data-interchange format
- JSON is language independent *
- JSON is "self-describing" and easy to understand
* The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. The code for reading and generating JSON data can be written in any programming language.
JSON Objects
JSON objects are written inside curly braces.
Just like in JavaScript, objects can contain multiple name/value pairs:
JSON Arrays
JSON arrays are written inside square brackets.
Just like in JavaScript, an array can contain objects:
{"firstName":"Deepak", "lastName":"Chahar"},
{"firstName":"Meenakshi", "lastName":"Nirman"},
{"firstName":"Susheel", "lastName":"Kumar"}
]
In the example above, the object "employees" is an array. It contains three objects.
Each object is a record of a person (with a first name and the last name).
Converting a JSON Text to a JavaScript Object
A common use of JSON is to read data from a web server and display the data on a web page.
For simplicity, this can be demonstrated using a string as input.
Then, use the JavaScript built-in function JSON.parse()
to convert the string into a JavaScript object: