JSON Examples: Objects, Arrays, and Nested Structures
Explore practical examples of JSON objects and arrays. This tutorial provides clear illustrations of JSON syntax, including nested objects and arrays, demonstrating how to represent complex data structures using this lightweight data-interchange format.
JSON Examples: Objects and Arrays
JSON Objects
A JSON object is a collection of key-value pairs. Keys are strings (enclosed in double quotes), and values can be any valid JSON data type (string, number, boolean, array, or another object). Objects are enclosed in curly braces {}
. Key-value pairs are separated by colons (:
) and commas (,
).
JSON Object Example
{
"employee": {
"name": "sonoo",
"salary": 56000,
"married": true
}
}
JSON Arrays
A JSON array is an ordered list of values enclosed in square brackets []
. The values can be any valid JSON data type. Items in the array are separated by commas.
JSON Array of Values
Code
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
JSON Array of Objects
Code
[
{ "name": "Ram", "email": "Ram@gmail.com" },
{ "name": "Bob", "email": "bob32@gmail.com" }
]
More Complex JSON Examples
Example 1
Code
{
"employees": [
{ "name": "Shyam", "email": "shyamjaiswal@gmail.com" },
{ "name": "Bob", "email": "bob32@gmail.com" },
{ "name": "Jai", "email": "jai87@gmail.com" }
]
}
Example 2
Code
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{ "value": "New", "onclick": "CreateDoc()" },
{ "value": "Open", "onclick": "OpenDoc()" },
{ "value": "Save", "onclick": "SaveDoc()" }
]
}
}
}
These examples demonstrate how objects and arrays can be nested to represent complex data structures.