JavaScript JSON Intro
JavaScript Basics

JavaScript JSON Intro

JSON - Introduction

JSON - Introduction

HTML and JSON

JSON stands for JavaScript Object Notation. It is a text format used for storing and transporting data. JSON is "self-describing" and easy to understand.

JSON Example

Here is an example of a JSON string:

'{"name":"John", "age":30, "car":null}'

This string defines an object with three properties:

  • name
  • age
  • car

Each property has a corresponding value.

If you parse this JSON string with a JavaScript program, you can access the data as an object:

let obj = JSON.parse('{"name":"John", "age":30, "car":null}');let personName = obj.name;let personAge = obj.age;‍

What is JSON?

  • JSON stands for JavaScript Object Notation.
  • JSON is a lightweight data-interchange format.
  • JSON is plain text written in JavaScript object notation.
  • JSON is used to send data between computers.
  • JSON is language-independent.

The JSON syntax is derived from JavaScript object notation, but the JSON format is text only. Code for reading and generating JSON exists in many programming languages. The JSON format was originally specified by Douglas Crockford.

Why Use JSON?

  • JSON format is syntactically similar to JavaScript objects, making it easy for JavaScript programs to convert JSON data into JavaScript objects.
  • JSON data, being text only, can be easily sent between computers and used by any programming language.
  • JavaScript has built-in functions for converting JSON strings into JavaScript objects using JSON.parse(), and for converting objects into JSON strings using JSON.stringify().

Example of converting JSON to JavaScript object:

let jsonString = '{"name":"John", "age":30, "car":null}';

let obj = JSON.parse(jsonString);

Example of converting JavaScript object to JSON string:

let obj = {name: "John", age: 30, car: null};
let jsonString = JSON.stringify(obj);

You can receive pure text from a server and use it as a JavaScript object. You can also send a JavaScript object to a server in pure text format, enabling seamless data exchange and manipulation without complicated parsing and translations.

Storing Data

When storing data, it must be in a specific format. Text is always one of the legal formats for storing data. JSON makes it possible to store JavaScript objects as text, ensuring compatibility and ease of access across different systems and programming languages.

Take a look into your desired course