
JSON Object
In JSON (JavaScript Object Notation), a JSON object is a fundamental data structure that represents an unordered collection of key-value pairs. It is similar to an associative array or dictionary in other programming languages. JSON objects are enclosed in curly braces {}
, and each key-value pair is separated by a colon :
. The keys are always strings, and the values can be any valid JSON data type, including strings, numbers, booleans, arrays, other objects, or even null values.
Syntax of a JSON Object:
{
"key1": value1,
"key2": value2,
...
}
Example of a Simple JSON Object:
{
"name": "John Doe",
"age": 30,
"isMarried": false,
"hobbies": ["reading", "swimming", "cooking"],
"address": {
"city": "New York",
"zipCode": "10001",
"country": "USA"
}
}
In this example, we have a JSON object representing information about a person. The object contains five key-value pairs:
"name": "John Doe"
: The key “name” is associated with the string value “John Doe.”"age": 30
: The key “age” is associated with the number value 30."isMarried": false
: The key “isMarried” is associated with the boolean value false."hobbies": ["reading", "swimming", "cooking"]
: The key “hobbies” is associated with an array containing three string values: “reading,” “swimming,” and “cooking.”"address": { ... }
: The key “address” is associated with another JSON object representing address details.
JSON objects can be nested within each other to create complex and hierarchical data structures, making it a powerful and flexible way to represent data. They are commonly used to organize and exchange data in web development, APIs, and various data interchange scenarios. JSON objects are a fundamental component of JSON and play a crucial role in making JSON a versatile and widely adopted data interchange format.