|
| 1 | +--- |
| 2 | +title: Is JSON really a subset of YAML? |
| 3 | +date: 2025-07-29 |
| 4 | +draft: false |
| 5 | +authors: [ingydotnet] |
| 6 | +categories: [Summer-of-YS] |
| 7 | +edit: blog/2025-07-29.md |
| 8 | +comments: true |
| 9 | +--- |
| 10 | + |
| 11 | +YAML is a superset of JSON. |
| 12 | + |
| 13 | +This is a basic truth about YAML. |
| 14 | + |
| 15 | +I assume that in 2025, most people who use YAML and JSON regularly already know |
| 16 | +this. |
| 17 | + |
| 18 | +Today I'll say a bit more about this and how it came to be. |
| 19 | + |
| 20 | +<!-- more --> |
| 21 | + |
| 22 | + |
| 23 | +## 2001, 2006 and 2009 |
| 24 | + |
| 25 | +The fact that JSON is a subset of YAML is more of a happy accident than |
| 26 | +something that was planned. |
| 27 | + |
| 28 | +I started working on YAML with Clark and Oren in April 2001. |
| 29 | + |
| 30 | +We didn't become aware of JSON until 2006. |
| 31 | +That seemed to be the same time that almost everyone was becoming aware of JSON. |
| 32 | + |
| 33 | +I was surprised to learn much later that JSON also started in 2001! |
| 34 | + |
| 35 | +By 2006, the YAML 1.1 spec had already been published. |
| 36 | + |
| 37 | +When we took a closer look at JSON, we realized that it was basically YAML's |
| 38 | +flow style collections with double quoted strings. |
| 39 | + |
| 40 | +When we looked _really_ closely at JSON, we realized that there were only 3 |
| 41 | +small issues that prevented JSON from being a subset of YAML. |
| 42 | + |
| 43 | +One of them was that the colon separator for key/value mapping pairs must have |
| 44 | +a space after it. |
| 45 | +JSON has no such requirement. |
| 46 | + |
| 47 | +We decided to take advantage of this situation and make JSON a proper subset of |
| 48 | +YAML for the 1.2 spec. |
| 49 | + |
| 50 | +For the colon thing we made the special rule that in a flow mapping, if the key |
| 51 | +is double quoted (which all JSON keys are), then the colon doesn't need |
| 52 | +whitespace after it. |
| 53 | + |
| 54 | +I forget the other two issues. |
| 55 | +It was a while ago! |
| 56 | + |
| 57 | +That came out in 2009. |
| 58 | + |
| 59 | + |
| 60 | +## But is JSON really 100% a subset of YAML? |
| 61 | + |
| 62 | +As far as we (the current core YAML dev team) know, yes, JSON is really 100% a |
| 63 | +subset of YAML. |
| 64 | + |
| 65 | +This has been challenged from time to time. |
| 66 | + |
| 67 | +Each time that it is challenged, we have to go back and look at the spec closely |
| 68 | +and check. |
| 69 | +So far we have fallen on the happy YAML side. |
| 70 | + |
| 71 | +It's entirely possible that there is a corner case that we have missed. |
| 72 | + |
| 73 | +IF someone finds one, I guess we'll have to put out a new YAML 1.3 spec. |
| 74 | + |
| 75 | + |
| 76 | +## The importance of JSON, YAML and YS |
| 77 | + |
| 78 | +JSON is a very simple and trustworthy format. |
| 79 | + |
| 80 | +It's hard to mess it up. |
| 81 | + |
| 82 | +It's super reliable for data that you don't have to look at or maintain. |
| 83 | + |
| 84 | +YAML is better for that. |
| 85 | + |
| 86 | +Since JSON is a subset of YAML, you can easily write and maintain data in YAML |
| 87 | +and then convert it to JSON for the machine. |
| 88 | + |
| 89 | +JSON plays an important role for YS too. |
| 90 | + |
| 91 | +YS considers JSON to be the Lingua Franca of data. |
| 92 | + |
| 93 | +When you load a file with YS, the return value is always a data structure |
| 94 | +of the JSON data model. |
| 95 | + |
| 96 | +In fact, a libys.so binding library for any given programming language currently |
| 97 | +has one function: |
| 98 | + |
| 99 | +The `load` function takes a YS/YAML/JSON string and returns a JSON string that |
| 100 | +is then decoded by the library into a native data structure for that language. |
0 commit comments