JSON means JavaScript Object Notation. As you might suspect by the name, JSON derives from JavaScript data formats. It is an open standard, lightweight, text-based format with limited data types. Its short syntax and simple structure make it very easy to read.
It’s used to send data to the server and from server to client, like an envelope in which the data is enclosed and sent back and forward between correspondents.
It’s highly appreciated by programmers who use it in mobile apps, database services like MongoDB or Kubernetes, and user interactions, being a more user-friendly alternative to XML. The simplicity and speed of processing that JSON offers and its high compatibility with any system make it one of the most popular formats in use today.
According to JSON's developer, Douglas Crockford, a language using the same principles was already being used at Netscape in 1996, but JSON only became mainstream as an established syntax in 2001.
It is a go-to resource for Android technology, REST-API, or any situation where data is sent from a server to a web page. It is language-independent, which means it can be used with any programming language. And a huge part of our online experience relies on it.
What are the defining features of a JSON document? JSON file extensions are defined as .json. It uses universal data structures and a limited choice of data types. Its simple syntax uses conventions familiar to programmers of the C-family of languages, like C, C++, C#, Java, JavaScript, Perl, Python, etc. As the official JSON website explains:
“An object is an unordered set of name/value pairs. An object begins with {left brace and ends with }right brace. Each name is followed by :colon, and the name/value pairs are separated by ,comma.”
JSON’s syntax rules are simple.
Let’s look at a JSON example, using some information about the famous heavy metal band Metallica.
As you can see, curly brackets delimit both the object and the arrays. All items are quoted, and the attribute name is separated from the value by a colon. A comma is used to separate name:value pairings.
Lists are defined by using square brackets, where the array attributes are also separated by commas.
YAML used to mean Yet Another Markup Language, but that meaning changed to YAML Ain't Markup Language to express its data focus over a document focus. Acronyms aside, YAML is a very human-readable, lightweight format, commonly used to store configuration information for DevOps tools like ElasticSearch, Docker, Kubernetes, Prometheus, and Ansible.
Like it says on the official website, also written in YAML format:
“YAML is a human-friendly data serialization language for all programming languages.”
It is a data serialization language, but it is also often used for writing configuration files. We’ll be looking at YAML through the data serialisation prism.
YAML was first publicised in 2001. The founding members of YAML are IngydotNet, Clark Evans, and Oren Ben-Kiki, who joined efforts to create a simpler format than XML. You can read the story from a firsthand perspective. Since then, it has become a workhorse for developers everywhere.
YAML is written in a very clear human-readable format, with the added advantage of supporting comments, making it super easy to edit. Since it is a superset of JSON, a valid YAML file can contain JSON objects. It is suited to support complex data types, with the ability to enclose multi-level objects. This makes it less friendly to use with some technologies.
YAML accepts the same data types as JSON, the main difference being the ability to support date attributes.
The absence of brackets and quotes for most of the functions makes the reading more natural. Even the uninitiated get a quick understanding of what is supposed to happen. YAML’s syntax is its strength but also a source for validation problems if we don’t pay attention to indents and space positions.
Now, see how the same data we used above for JSON looks in YAML format:
On a first impression, YAML looks better than JSON since it gets rid of all the brackets and quotes. It doesn’t read much like code but more as an outline.
YAML and JSON are two popular languages, similar in structure and usability. Their differences in design, syntax, and functionality make the choice between them a matter of purpose.
When choosing between JSON or YAML format, just follow function: what do you need them to do?
JSON has a faster delivery since it works perfectly as a simple data exchange format. But it is limited to the supported data types, which can require extra resources to deal with data sets with a larger number of data type options. It is easy to read once you’re over the brackets and quotes, which is an advantage since they delimit specific items, reducing the risk of parsing misinterpretation.
YAML, on the other hand, almost doesn’t read like code. And the matryoshka-like ability to include objects within objects allows for greater data complexity. The downside? Such complexity slows down the parsing and generation process. But with a range of complex data types available, it has its appeal and is a great option to expand the types of information to be made available.
The purpose and priority of the information to deliver are also important, and a logical hierarchy should be defined before starting coding. This prior organization will define the structure of the document and how data will be presented.
No matter the choice, it’s all about what your data is supposed to do in the end.
Content writer and digital media producer with an interest in the symbiotic relationship between tech and society. Books, music, and guitars are a constant.
People who read this post, also found these interesting: