In this quick article, we’ll take a look at the differences between YAML and JSON through quick and practical examples.
To have a better image, let’s start by looking at the JSON and YAML representations of a simple POJO:
class Person {
String name;
Integer age;
List<String> hobbies;
Person manager;
}
First, let’s look at its JSON representation:
{
"name":"John Smith",
"age":26,
"hobbies":[
"sports",
"cooking"
],
"manager":{
"name":"Jon Doe",
"age":45,
"hobbies":[
"fishing"
],
"manager":null
}
}
JSON syntax is somewhat cumbersome as it uses special syntax like curly braces {} and square brackets [] to represent objects and arrays.
Next, let’s see how the same structure would look in YAML:
name: John Smith
age: 26
hobbies:
- sports
- cooking
manager:
name: Jon Doe
age: 45
hobbies:
- fishing
manager:
YAML’s syntax looks a bit friendlier as it uses blank spaces to denote relations between objects and ‘–‘ to represent array elements.
We can see that although both are easily readable, YAML tends to be more human-readable.
Another bonus point for YAML is the number of lines it takes to represent the same information — YAML takes only 11 lines, while JSON takes 16.
#data #json #javascript #developer