JSON Objects

Nested JSON Objects

Values in a JSON object can be another JSON object.

You can access nested JSON objects by using the dot notation or bracket notation:

Modify Values

You can use the dot notation to modify any value in a JSON object:

You can also use the bracket notation to modify a value in a JSON object:

Delete Object Properties

Use the delete keyword to delete properties from a JSON object:

JSON Arrays

Arrays as JSON Objects

Arrays in JSON Objects

Arrays can be values of an object property:

Accessing Array Values

You access the array values by using the index number:

Looping Through an Array

You can access array values by using a for-in loop:

Or you can use a for loop:

Nested Arrays in JSON Objects

Values in an array can also be another array, or even another JSON object:

To access arrays inside arrays, use a for-in loop for each array:

Delete Array Items

Use the index number to modify an array:

Modify Array Values

Use the delete keyword to delete items from an array:


Example – Parsing JSON

Imagine we received this text from a web server:

Use the JavaScript function JSON.parse() to convert text into a JavaScript object:

Use the JavaScript object in your page:

JSON From the Server

You can request JSON from the server by using an AJAX request

As long as the response from the server is written in JSON format, you can parse the string into a JavaScript object.

Array as JSON

When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.

댓글 남기기