JSON

JSON stands for JavaScript Object Notation which is a lightweight format for exchanging data. It is commonly used for transmitting data between a server and a web application, as an alternative to XML. JSON is easy to read and write, and it is almost like writing regular JavaScript objects.

Syntax

JSON has a simple syntax, and it is written in key-value pairs, where the keys are strings, and the values can be any valid JSON data types such as string, number, boolean, null, array or another JSON object.

{
  "name": "John",
  "age": 30,
  "isAdmin": true,
  "hobbies": ["reading", "drawing", "coding"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  }
}

JSON data types

JSON supports the following data types:

  • String: A sequence of characters, wrapped in double quotes.

  • Number: A numeric value, which can be integer, float or exponential.

  • Boolean: A value which can be either true or false.

  • Null: A value which represents null or empty.

  • Array: A collection of values, enclosed in square brackets and separated by commas.

  • Object: A collection of key-value pairs, enclosed in curly braces and separated by commas.

Parsing JSON

In JavaScript, JSON can be parsed into a JavaScript object using the JSON.parse() method:

Converting to JSON

In JavaScript, an object can be converted into a JSON string using the JSON.stringify() method:

Conclusion

JSON is a lightweight, easy-to-read and easy-to-write data format, and it is widely used in web applications. It supports various data types, and it can be easily parsed and converted to other data formats.

Last updated