As I browse through numerous JSON tutorials online, including those on STO, I find myself in a state of confusion when it comes to determining the right approach for writing an external JSON file.
I have come across various examples such as:
(although Adobe Dreamweaver CS5.5 shows syntax errors for a single object, the reason is unclear)
#1
{
"name" : "Sachin",
"age" : 30,
"country" : "India"
}
#2
For multiple objects:
[
{
"name" : "Sachin",
"age" : 30,
"country" : "India"
},
{
"name" : "Amit",
"age" : 28,
"country" : "USA"
}
]
#3
Some tutorials suggest wrapping the objects array in single quotes and storing it in a variable like this:
customers = '[
{
"name" : "Sachin",
"age" : 30,
"country" : "India"
},
{
"name" : "Amit",
"age" : 28,
"country" : "USA"
}
]'
#4
Others prefer formatting the code in the following manner:
{
"customers" : [
{
"name" : "Sachin",
"age" : 30,
"country" : "India"
},
{
"name" : "Amit",
"age" : 28,
"country" : "USA"
}
]
}
#5
An additional sample format looks like this:
{
[
{
"name" : "Sachin",
"age" : 30,
"country" : "India"
},
{
"name" : "Amit",
"age" : 28,
"country" : "USA"
}
]
}
Frankly speaking, I am completely confused and unable to determine which style is correct and standard for writing an external .json file (especially when dealing with multiple objects).
Therefore, I bring all my questions here:
- What distinguishes each of the formats mentioned above? Should I use single quotes, store the data in a variable, or assign a key to the entire dataset?
- How can I create a properly formatted .json file that can be easily interpreted by JavaScript and PHP?
- In what standardized format do third-party APIs typically present JSON data?