YAML compatible JSON

As part of a new project, I’ve been authoring some files (by hand) in JSON. I chose JSON because it is simple, more than capable enough for our usage and seems to have broad support in terms of parsers.

Though there are parsers in many languages, I’m using Ruby, which has an even better parser for YAML than for JSON. It’s just more convenient and useful for me to use the YAML parser than the JSON parser. (if this project where ruby-only, I’d just use YAML, but JSON helps make the data cross-platform)

Anyway, as Mr. The Lucky Stiff has pointed out, JSON is YAML. At least, some JSON is YAML. Of course, _why later found out that there’s actually some subtle differences which can make YAML and JSON incompatible. The maintainers of both formats seem to be working together to resolve these differences, but in the meantime, it’d be nice to be able to make a subset of the formats work together.

I think I’ve figured out how to write JSON documents which are also valid YAML. Here are my notes:

  • Quote all strings. YAML allows unquoted strings, but this won’t parse in JSON.

    yes:

    {"foo": "bar"}

    no:

    {"foo": bar}
  • Put a space after, but not before colons.

    yes:

    {"foo": "bar"}

    no:

    {"foo" : "bar"}

What other problems have you run into? What suggestions do you have?

2 Responses to “YAML compatible JSON”

  1. assaf Says:

    Why not just use a JSON parser?

    http://labnotes.org/svn/public/lws/lib/json/

  2. ryan Says:

    Because I already have a YAML parser?