JSON (JavaScript Object Notation) It is a lightweight and widely used data exchange format for creating asynchronous web applications. It is used to represent structured information, always in the form of text that is readable by both humans and the servers with which it communicates, in .json format.
It is often used for working with AJAX as an alternative to the XML format, as the latter is bulkier and therefore less practical for facilitating the rapid exchange of information between servers and users’ computers.
It is worth noting that JSON is a very versatile format, as it enables communication between two programmes that may be written in completely different programming languages, such as Java and Python.
Major companies such as Twitter, Facebook (now Meta), Yahoo and Google opt for the JSON format across all their online services.
What is JSON used for?
JSON is commonly used to transmit data between web applications and services via HTTP as part of the API (Application Programming Interfaces).
It is particularly useful in the development of web and mobile applications because it is easy for both humans and computer systems to understand and process. Furthermore, thanks to its lightweight format, it is more efficient in terms of data transmission than other, more verbose formats, such as XML.
In addition, JSON is also used to facilitate communication between different components of distributed systems and exchange data within systems of the the Internet of Things. Furthermore, JSON files are also useful for storing data temporarily.
The main data structures generated using JSON are:
Objects
These are unordered collections of key-value pairs, where the keys are strings and the values can be any valid data type, such as numbers, strings, Booleans (false or true), nested objects or even arrays.
Example of a JSON object:
{
«name»: «Juan»,
«age»: 30,
«married»: false,
«hobbies»: [«football», «reading», «travelling»]
}
Arrays
Also known as vectors, these are ordered lists of values, where each value can itself be a JSON object, a string, a number, a Boolean or even another array.
Example of an array in JSON:
[
{
«name»: «Ana»,
«age»: 25
},
{
«name»: «Pedro»,
«age»: 28
},
{
«name»: «Luisa»,
«age»: 22
}
]
JSON or XML?
The choice between JSON and XML depends on the context and the specific requirements of each application or project. Both formats have their advantages and disadvantages, so it is important to consider various factors before deciding which is best in a particular case.
Advantages of JSON
- It is lighter and has a simpler structure than XML.
- It is easier for humans to read and write.
- It is closely related to JavaScript, so they can be easily integrated.
- It is widely used by most modern web APIs.
Advantages of XML
- It allows for a more complex and hierarchical structure that is better suited to representing complex data and documents with additional metadata.
- XML supports validation using schemas (XSD) to ensure the integrity and consistency of the data.
- It has a longer track record and is more widely established across a wide variety of platforms and programming languages.