Json Intro in JavaScript
JSON Introduction in JavaScript
What is JSON?
JSON (JavaScript Object Notation) is a lightweight format for storing and exchanging data. It is easy for humans to read and write, and easy for machines to parse and generate.
JSON is text-based and language-independent.
Commonly used to send data between a server and web application.
Derived from JavaScript object syntax but can be used with many programming languages.
JSON Structure
Data is represented as key/value pairs inside curly braces
{}— called a JSON object.Arrays
[ ]hold ordered lists of values.Values can be:
Strings (in double quotes)
Numbers
Booleans (
true/false)Null (
null)Objects
Arrays
Example JSON
{ "name": "John", "age": 30, "isStudent": false, "courses": ["Math", "Physics"], "address": { "city": "New York", "zip": "10001" }}Working with JSON in JavaScript
Parsing JSON (string ? JavaScript object)
const jsonString = '{"name":"John","age":30}';const obj = JSON.parse(jsonString);console.log(obj.name); // Output: Johnconsole.log(obj.age); // Output: 30Stringifying JavaScript object (object ? JSON string)
const person = {name: "John", age: 30};const jsonStr = JSON.stringify(person);console.log(jsonStr); // Output: '{"name":"John","age":30}'Why Use JSON?
Easy to transmit data between client and server.
Can be parsed and manipulated easily in JavaScript.
Supported by almost all programming languages and systems.
If you want, I can show you how to fetch JSON from a web API and use it in your JavaScript code!