From 7125e17b9660a479c1d9627850024934c7047782 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 24 Aug 2021 19:42:45 -0500 Subject: [PATCH] Add YAML Is A Superset Of JSON as a YAML til --- README.md | 3 ++- yaml/yaml-is-a-superset-of-json.md | 39 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 yaml/yaml-is-a-superset-of-json.md diff --git a/README.md b/README.md index 529b527..381eb01 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1146 TILs and counting..._ +_1147 TILs and counting..._ --- @@ -1347,6 +1347,7 @@ _1146 TILs and counting..._ ### YAML - [Create Multi-Line Strings Without The Line Breaks](yaml/create-multi-line-strings-without-the-line-breaks.md) +- [YAML Is A Superset Of JSON](yaml/yaml-is-a-superset-of-json.md) ## Usage diff --git a/yaml/yaml-is-a-superset-of-json.md b/yaml/yaml-is-a-superset-of-json.md new file mode 100644 index 0000000..4f8347d --- /dev/null +++ b/yaml/yaml-is-a-superset-of-json.md @@ -0,0 +1,39 @@ +# YAML Is A Superset Of JSON + +Put another way, JSON is a subset of YAML. + +Here is what the YAML spec has to say: + +> YAML can therefore be viewed as a natural superset of JSON, offering improved +> human readability and a more complete information model. This is also the +> case in practice; every JSON file is also a valid YAML file. + +We can _see_ this in practice by using our favorite YAML parse to read a JSON +file. For me that is Ruby and its built-in YAML library. + +First, consider the following JSON file (`data.json`): + +```json +{ + "data": [ + "one", + "two", + "three" + ], + "number": 22 +} +``` + +Now, I'll open up an IRB session (Ruby's interactive REPL) and read it in. + +```ruby +> require 'yaml' +=> true +> YAML.load_file('data.json') +=> {"data"=>["one", "two", "three"], "number"=>22} +``` + +It works. And for me, having not considered those two format related, this +isn't something I would have expected to work. + +[source](http://yaml.org/spec/1.2/spec.html#id2759572)