3 ms·
despite how popular this claim is, it is not true. The following is a valid JSON document according to the spec[1]: { "key": "value", "key": "otherValue" }
by dimitropoulos 6y ago
despite how popular this claim is, it is not true.
The following is a valid JSON document according to the spec[1]:
{ "key": "value", "key": "otherValue" }
But is not a valid YAML document[2], something addressed in the spec itself. The JSON spec says
> The names within an object SHOULD be unique.
And `SHOULD` is defined by RFC 2119[3] as
> This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.
Also note that popular JSON validators exist which will accept the above example as completely valid[4]. Also note that Javascript's JSON.parse[5] will accept the above as valid, as does JavaScript itself for object keys (which, yes, is not JSON).
Yes, I know about RFC 8259 vs ECMA 404. The real world examples noted above, though, speak for themselves.
[1] https://tools.ietf.org/html/rfc4627 https://tools.ietf.org/html/rfc4627
[2] https://yaml.org/spec/1.2/spec.html https://yaml.org/spec/1.2/spec.html
[3] https://tools.ietf.org/html/rfc2119 https://tools.ietf.org/html/rfc2119
[4] https://www.freeformatter.com/json-validator.html https://www.freeformatter.com/json-validator.html
[5] https://www.ecma-international.org/ecma-262/#sec-internalizejsonproperty https://www.ecma-international.org/ecma-262/#sec-internalize...
- jchw 6y agoHey dimitri, what's up. JSON is not 100% a subset of YAML, this is true; that's slightly a simplification of the truth that is commonly echoed. It is a syntactically a subset of YAML, and for anything representable in JSON you can still do a functionally-lossless translation* from JSON to YAML and back again. This is because: > In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten. So, { "key": "value", "key": "otherValue" } becomes { "key": "otherValue" } in a round trip, but represents the same thing, going by ECMA262 rules. These are definitely distinct snippets of JSON and if you only use RFC4627 then it doesn't define anything about how to parse the JSON beyond the syntax, whereas the YAML spec is a bit more complete in regards to specifying semantics probably partly since it's a more complicated language. Thankfully in the common case most JSON parsers do as specified in ECMA262 (which makes sense since that's where this format came from anyways) and use the latest key. You can see this in Go's "encoding/json" for example. So this property is still meaningfully true, I'd argue, even if it is more nuanced than commonly believed. tl;dr: It is true that not all valid JSON is valid YAML. However, automatic translation between the JSON and YAML is still possible if you are going by ECMA262 JSON semantics. P.S.: In practice, YAML parsers do actually often take the liberty of interpreting YAML to be a proper superset. This isn't too surprising since most YAML parsers take a lot of liberties that are/were outright spec violations, but just as an example, here's PyYAML: >>> from yaml import load >>> load('{"a":"1","a":2}') {'a': 2}