Understanding JSON Objects: Structure, Syntax, and Data Types
Learn the fundamentals of JSON objects, a key data structure in JSON (JavaScript Object Notation). This tutorial explains the structure of JSON objects (key-value pairs), supported data types (string, number, boolean, array, nested objects), and provides clear examples to solidify your understanding of JSON syntax.
Understanding JSON Objects
What is a JSON Object?
A JSON object is a collection of key-value pairs. Each key is a string (enclosed in double quotes), and each value can be a string, number, boolean, array, or another JSON object (allowing for nested objects). Objects are enclosed in curly braces {}
, and key-value pairs are separated by colons (:
) and commas (,
).
JSON Object Examples
Here are some examples demonstrating different data types within a JSON object:
Simple JSON Object
Code
{
"employee": {
"name": "sonoo",
"salary": 56000,
"married": true
}
}
This shows a nested object; the "employee" is an object itself, containing key-value pairs for name, salary (a number), and marital status (a boolean).
JSON Object with Strings
Code
{
"name": "sonoo",
"email": "sonoojaiswal1987@gmail.com"
}
JSON Object with Numbers
Code
{
"integer": 34,
"fraction": 0.2145,
"exponent": 6.61789e+0
}
JSON Object with Booleans
Code
{
"first": true,
"second": false
}
Nested JSON Object
Code
{
"firstName": "Sonoo",
"lastName": "Jaiswal",
"age": 27,
"address": {
"streetAddress": "Plot-6, Mohan Nagar",
"city": "Ghaziabad",
"state": "UP",
"postalCode": "201007"
}
}