Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between YAML and JSON?

Tags:

json

yaml

What are the differences between YAML and JSON, specifically considering the following things?

  • Performance (encode/decode time)
  • Memory consumption
  • Expression clarity
  • Library availability, ease of use (I prefer C)

I was planning to use one of these two in our embedded system to store configure files.

Related:

Should I use YAML or JSON to store my Perl data?

like image 684
pierrotlefou Avatar asked Nov 13 '09 02:11

pierrotlefou


People also ask

What is better YAML or JSON?

JSON is comparatively faster than YAML. However, if data configurations are small then YAML is better since its interface is much more friendly. JSON has a feature to encode six different data types like an object, array, strings, numbers, null and boolean.

Is all JSON YAML?

One thing that most people might not know is that YAML is a superset of JSON. This means that any JSON is a valid YAML file! YAML just extends the JSON syntax to provide more features (like comments etc) and alternatives to represent the same data structures.


1 Answers

Technically YAML is a superset of JSON. This means that, in theory at least, a YAML parser can understand JSON, but not necessarily the other way around.

See the official specs, in the section entitled "YAML: Relation to JSON".

In general, there are certain things I like about YAML that are not available in JSON.

  • As @jdupont pointed out, YAML is visually easier to look at. In fact the YAML homepage is itself valid YAML, yet it is easy for a human to read.
  • YAML has the ability to reference other items within a YAML file using "anchors." Thus it can handle relational information as one might find in a MySQL database.
  • YAML is more robust about embedding other serialization formats such as JSON or XML within a YAML file.

In practice neither of these last two points will likely matter for things that you or I do, but in the long term, I think YAML will be a more robust and viable data serialization format.

Right now, AJAX and other web technologies tend to use JSON. YAML is currently being used more for offline data processes. For example, it is included by default in the C-based OpenCV computer vision package, whereas JSON is not.

You will find C libraries for both JSON and YAML. YAML's libraries tend to be newer, but I have had no trouble with them in the past. See for example Yaml-cpp.

like image 129
AndyL Avatar answered Sep 17 '22 05:09

AndyL