Json Syntax in JavaScript
JSON Syntax in JavaScript
What is JSON?
JSON (JavaScript Object Notation) is a lightweight format for storing and exchanging data.
It is text-based and easy for humans to read and write.
JSON is language-independent but uses conventions familiar to JavaScript.
Basic JSON Syntax Rules
| Rule | Description / Example |
|---|---|
| Data is in name/value pairs | "name": "John" |
| Data is separated by commas | "name": "John", "age": 30 |
| Curly braces hold objects | { "name": "John", "age": 30 } |
| Square brackets hold arrays | [ "apple", "banana", "cherry" ] |
| Names (keys) must be double-quoted strings | "name" (not single quotes or unquoted) |
| Values can be: | String, Number, Object, Array, Boolean, or null |
| Strings must be in double quotes | "hello" (not 'hello') |
Example of a Valid JSON Object
{ "firstName": "Jane", "lastName": "Doe", "age": 25, "isStudent": true, "hobbies": ["reading", "swimming", "coding"], "address": { "street": "123 Main St", "city": "New York", "postalCode": "10001" }}Common Mistakes to Avoid
| Invalid JSON | Reason |
|---|---|
{name: "John"} | Keys must be double-quoted strings |
{'name': 'John'} | Keys and strings must use double quotes |
{"age": 30,} | No trailing commas after last pair |
{"isStudent": Yes} | Boolean values are lowercase (true or false) |
Using JSON in JavaScript
Parse JSON string to JavaScript object:
const obj = JSON.parse(jsonString);Convert JavaScript object to JSON string:
const jsonString = JSON.stringify(obj);If you want, I can help you validate or generate JSON syntax for your data!