JSON Structure
The JSON format consists of two pairs of braces {} and [] that contain the structure where each attribute and non-numeric value will be entered between quotes { “attribute”: “value” } , separated by commas to group more attributes and values { “attribute”: “value”, “attribute” : “value”, “attribute: “value” }. Depending on the complexity of the object to be encoded, the structure may or may not have values.
How to apply the JSON format to an “object”?
To apply a JSON format to an object, it is necessary to organize the content so that each attribute has values, whether null, text strings, integers, or arrays. For example, if the object presents the following data:
User 1
First Name: Pedro
Last Name: Pérez
User: Pedrope
Age: 18
Country: Chile
In JSON format, we should enter:
{
"user1":
{
"firstname":"pedro",
"lastname":"perez",
"user":"pedrope",
"age":18,
"country":"chile"
}
}
If you need to add more objects (another user in this case), just add another structure separated by commas:
{
"user1":{
"firstname":"pedro",
"lastname":"perez",
"user":"pedrope",
"age":18,
"country":"chile"
},
"user2":{
"firstname":"juan",
"lastname":"gonzalez",
"user":"juangonza",
"age":20,
"country":"argentina"
}
}
The values in our JSON structure can also be arrays, defined using brackets [ ]:
[1,2,3,4,5,6]
Conclusions
The JSON format is ideal for exchanging data quickly and effectively between servers due to its lightness. If we take the example presented above and present it in XML format, we can better appreciate the character synthesis required by the JSON format:
18
perez
chile
pedro
pedrope
20
gonzalez
argentina
juan
juangonza
Of course, the more data you need to share, the more valuable this feature will be.
Despite JSON's efficiency when working with structured data, there are still private and state institutions that continue to use XML, such as the Internal Revenue Service, through their developer APIs.