Explain the syntax of JSON with an example.
Answered
Explain the syntax of JSON with an example.
Best answer
JSON has the following syntax:
- Objects are enclosed in braces ({}), their name-value pairs are separated by a comma (,), and the name and value in a pair are separated by a colon (:). Names in an object are strings, whereas values may be of any of the data types, including another object or an array.
- Arrays are enclosed in brackets ([]), and their values are separated by a comma (,). Each value in an array may be of a different type, including another array or an object.
- When objects and arrays contain other objects or arrays, the data has a tree-like structure.
The following example shows JSON data for a sample object that contains name-value pairs. The value for the name “phoneNumbers” is an array whose elements are two objects.
{
“firstName”: “James”,
“lastName”: “Jackson”,
“age”: 52,
“address”: “Kaiser Drive”,
“city”: “Fremont”,
“state”: “CA”,
“postalCode”: “94555”,
“phoneNumbers”: [
{ “Mobile”: “111-111-1111” },
{ “Landline”: “222-222-2222” }
]
}