6 ms·
JSON is a subset of YAML, so if you want to write YAML like JSON you can, and you can even automatically convert from JSON style to more idiomatic YAML, or vice
by dragonwriter 6y ago
JSON is a subset of YAML, so if you want to write YAML like JSON you can, and you can even automatically convert from JSON style to more idiomatic YAML, or vice-versa, with appropriate tooling.
- ddevault 6y agoCorrection: YAML is a superset of JSON
- ben-schaaf 6y agoTo explain the downvotes: JSON ⊆ YAML implies YAML ⊇ JSON. You're saying the equivalent of "3 > 1 is wrong: 1 < 3".
- doteka 6y agoWhat Drew is saying is absolutely correct though. YAML was specifically designed to be a superset of JSON. Conversely, when JSON was designed, YAML did not exist, so it only became a subset of YAML when the latter was introduced.
- jerf 6y agoThe words subset and superset do not imply any temporal relationship that somebody could get wrong.
- saagarjha 6y agoThe first time I heard that, I thought it was insane. I think it's still pretty insane.
- erik_seaberg 6y agoThat works if all your YAML authors are carefully avoiding any use of the features JSON can’t represent, like multiple documents, non-string map keys, and application-defined type tags.
- dragonwriter 6y ago> That works if all your YAML authors are carefully avoiding any use of the features JSON can’t represent, like multiple documents, non-string map keys, and application-defined type tags. That's true if you want round-trip conversion to and from JSON, but you don't need that for automated round-trip conversion between JSON-style (delimited, or, in the language of the YAML spec “flow style”) YAML and the somewhat more idiomatic for most uses indented, (or, in the spec language, “block style") YAML, e.g., if you find it easier to edit in flow-style but you are in an environment that wants the end product delivered in block style. Notably, whitespace sensitivity is a feature of block style, but not flow style.
- dimitropoulos 6y agodespite 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}
- thunderbong 6y agoWhen I read this, I didn't believe it. I needed to prove this to myself! I've seen and used YAML files for configurations in many places and I've always found editing YAML files by hand too much of a pain. It's very easy to miss a space and screw everything up. However, writing JSON directly into a plain text file is totally easy. So, I created a normal text file named 'array_of_objects.json' like this - [ { "first": "one" }, { "second": "two" } ] And then I wrote a ruby program to read it - require 'json' require 'yaml' array_of_objects = YAML.load_file 'array_of_objects.json' p array_of_objects And I see the output as - [{"first"=>"one"}, {"second"=>"two"}] I still can't get over it. I'm thinking of the countless hours wasted!! Thank you so much.