JavaScript JSON (JavaScript Object Notation)
Subject: JavaScript
JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
Why Use JSON?
- Language-independent and lightweight.
- Easy to convert from JavaScript objects.
- Works seamlessly with APIs and RESTful services.
- Readable and structured format for configuration or data storage.
JSON Syntax Rules
- Data is in name/value pairs: "name": "value"
- Data is separated by commas.
- Curly braces
{}hold objects. - Square brackets
[]hold arrays. - Strings must be in double quotes.
- Only number, string, boolean, null, array, and object are allowed.
Example: JSON Data
Converting Between JSON and JavaScript
1. JSON to JavaScript Object (Parsing)
2. JavaScript Object to JSON (Stringifying)
Example: JSON in API Response
Limitations of JSON
- Cannot store functions or
undefined. - No support for circular references.
- Date objects are converted to strings.
Key Takeaways
- JSON is used for data exchange and configuration.
- Use
JSON.stringify()to convert a JavaScript object to a JSON string. - Use
JSON.parse()to convert a JSON string to a JavaScript object. - JSON syntax is strict: only double-quoted strings and basic data types.
- Ideal for APIs, AJAX, storage, and server communication.