JSON to YAML: Keep IDs and Booleans From Changing Type

Some strings look like numbers, booleans or null. The converter can quote those values in YAML to preserve their types. Confirm with the parser that will consume your file.

Worked example

JSON input

{
  "ticket": "0012",
  "flag": "false",
  "reviewed": false,
  "code": "null"
}

Actual YAML output

ticket: "0012"
flag: "false"
reviewed: false
code: "null"

Steps

  1. Start with valid JSON and put double quotes around values intended to be strings.
  2. Convert to YAML. Check that strings which resemble other types are quoted where necessary.
  3. Convert the YAML back to JSON or load it in the intended YAML parser. Compare both values and types for identifiers and flags.

What to check

  • The YAML shown above distinguishes the quoted string false from an unquoted false boolean.
  • YAML implementations can use different schemas. Validate in the parser that loads the production file.

Check the JSON and YAML round-trip

References