What is JSON? #
JSON stands for JavaScript Object Notation. It is an open standard data interchange format that applications and systems use to describe and exchange text-based data between databases, servers and applications.
Data transmitted over the web needs to be structured, and one of the most prominent formats these days is JSON.
Created as an alternative to XML in the early 2000s, JSON is based on a subset of the JavaScript Programming Language which is used as a data interchange for front end or backend applications.
It is now widely used for creating an API or consuming an API across the internet or within applications.
JavaScript Object Notation is a lot easier to read and write in comparison to many other formats, especially XML, as it is much cleaner in construction with not as many opening and closing tabs, making it easier for humans to actually read.
JSON natively supports strings, numbers, booleans, null, arrays and objects (key and value), which can be nested within a hierarchy of data instead of the flat format of data that most languages use. These fields can be easily added, removed or updated making it very flexible.
It is also extremely lightweight when transmitting data across a network due to its file size, and it can easily map directly to domain objects, making it very appealing to developers.
What is JSON used for? #
Almost every major language has some sort of library or built-in functionality to parse JSON strings into objects or classes which makes it extremely versatile and useful for data exchanges between different programming languages.
Some common use cases for JSON include:
- API responses: When a client makes a request to a web API, the server may respond with JSON data that the client can then parse and use.
- Configuration files: JSON is often used for configuration files because it is easy to read and write, and can be easily converted to a JavaScript object.
- Data storage: JSON can be used to store data in databases or files, as it is a text-based format that can be easily written and read by humans and computers.
- AJAX: AJAX (Asynchronous JavaScript and XML) is a technique for creating dynamic web applications, and JSON is commonly used as the data format for AJAX requests and responses.
Simple JSON example #
The following is a simple example of a JSON object:
{
"name": "Steve",
"age": 30,
"town": "Bournemouth",
"hobbies": ["music", "coding", "cylcing"],
"isMarried": false,
"car": null
}
In this example, the JSON object has several key-value pairs, including the person’s name, age, and city, as well as an array of hobbies. The boolean value isMarried is set to false, and the key car is set to null. Note that all keys and string values must be surrounded by double quotes in JSON.