Json Data Types in JavaScript
JSON Data Types in JavaScript
JSON (JavaScript Object Notation) supports a limited set of data types, which correspond closely to JavaScript data types.
JSON Data Types
| JSON Type | Description | Example | JavaScript Equivalent |
|---|---|---|---|
| String | Text wrapped in double quotes | "Hello World" | String |
| Number | Numeric values (integer or floating-point) | 42, 3.14 | Number |
| Boolean | true or false | true, false | Boolean |
| Null | Represents a null value | null | null |
| Array | Ordered list of values inside [ ] | [1, "two", false] | Array |
| Object | Collection of key/value pairs inside { } | {"name":"John", "age":30} | Object |
Examples of JSON Data Types
{ "name": "Alice", // String "age": 25, // Number "isStudent": false, // Boolean "hobbies": ["reading", "gaming"], // Array "address": { // Object "city": "New York", "zip": "10001" }, "spouse": null // Null}Using JSON Data Types in JavaScript
Parsing JSON String to JavaScript Object
const jsonString = `{ "name": "Alice", "age": 25, "isStudent": false, "hobbies": ["reading", "gaming"], "address": {"city": "New York", "zip": "10001"}, "spouse": null}`;const obj = JSON.parse(jsonString);console.log(obj.name); // "Alice"console.log(obj.age); // 25console.log(obj.isStudent); // falseconsole.log(obj.hobbies[0]); // "reading"console.log(obj.address.city);// "New York"console.log(obj.spouse); // nullConverting JavaScript Object to JSON String
const person = { name: "Alice", age: 25, isStudent: false, hobbies: ["reading", "gaming"], address: {city: "New York", zip: "10001"}, spouse: null};const jsonStr = JSON.stringify(person);console.log(jsonStr);If you'd like, I can help with examples on how to work with each type in real code or how to validate JSON data!