9

YAML appears to be more readable than a JSON formatted object, apart from readability what advantages or disadvantages does PyAML have instead of JSON? How should we make a decision between the two, note that I am not asking the difference between the two but recommendations on why and when to use one over the other?

3
  • 2
    Here's a most acceptable answer to that question in this forum: stackoverflow.com/questions/1726802/…
    – Vivek
    Commented Aug 23, 2013 at 8:16
  • Next time please looks out for such answered questions, this could earn you some negative points here...!!!
    – Vivek
    Commented Aug 23, 2013 at 8:17
  • 5
    I don't see a comparable question. Others ask what the technical difference is, not when one is more suitable than the other in practice. Commented Apr 13, 2018 at 22:38

2 Answers 2

6

JSON is more formal format than YAML. IMHO:

    YAML is better for fast creation and understanding of simple configuration files of software modules;
    JSON is better for fast implementation and implementation of simple data transfering between software modules.

1

There are one or two big differences between YAML and JSON that can (should) be stated quickly that are missing (or not prominent) in comprehensive answers like the one mentioned in the question comment.

YAML is indent defined

While YAML does not permit TAB characters for indentation, it is still an indent defined format. If you think your files may get mishandled and squashed around from a white space perspective (e.g. copy pasted off the web) you may prefer JSON.

JSON does not support numeric keys

You can specify a JSON key which contains only numbers but the key will still be a string and will require a convention or a format specification if that's going to get transformed into an actual int within your language. If you are going to need genuine numeric keys you may prefer YAML.

YAML is a superset of JSON

I think I'm right to say all valid JSON is valid YAML, even as far as accepting TAB characters in JSON sections which are not indent defined. If you can afford to use a YAML parser then you've got JSON as well if you want that restriction.

JSON is more widely available

Even now I think it's fairly incontrovertible to say that you can find JSON support almost everywhere, so choosing JSON will maximise your compatibility.

Have you thought about TOML

So TOML is a great candidate:

  • not so much compatibility, support not as omnipresent as JSON or even YAML
  • not indent defined, as robust as JSON from that perspective
  • can accept numeric keys but like JSON they will still parse as strings
  • line based format is compact, readable and unconfusing

If you can afford to lose the universal support this is a great alternative to YAML, offering some nifty compact hash table definition alternatives, with the robustness of a non-indent format like JSON, but, sadly, still no genuine numeric keys.

XML

Sorry Minas, I'm not going to say anything nice about XML today.

Not the answer you're looking for? Browse other questions tagged or ask your own question.