YAML to JSON and JSON to YAML

Paste YAML or open a .yaml file to get JSON, or switch the direction and turn JSON into YAML. Anchors are expanded, several documents become a list, and the text is converted in your browser tab, not uploaded.

YAML to JSON and JSON to YAML

How to convert YAML to JSON

  1. Leave Direction on YAML to JSON. Paste your YAML into the box, or click Open a YAML file and choose a .yaml, .yml or .txt file.
  2. Read the line under the box. It says Converted, or how many documents were found, or what is wrong and on which line.
  3. Tick or untick Indent the JSON depending on whether you want readable output or a single line.
  4. Use Copy JSON or Download .json. The file is saved as converted.json.
  5. For the other direction, change Direction to JSON to YAML and paste JSON. The download is converted.yaml.

What the converter does

The page reads YAML with js-yaml, a common library, and writes JSON. It fetches that library from the cdnjs network the first time you paste, type or open something, and the browser checks it against a fixed hash before running it. In our request log that was one GET with no body, and no request contained the text we entered. The page also loads Google ad and analytics scripts like every GramFile page; we blocked them for the test. Only Chromium was tested.

Our 187-byte sample (a name, a version, a list of two maps, a block of text and an anchor) came out as 325 characters of indented JSON, or 200 as one line. A blank box gives no output and no message.

Values that change type

The page follows the plain YAML 1.2 rules, so yes, no, on, off, y and n stay as text. Our test also showed that Yes stays text while true, True and TRUE become true. Tools that follow YAML 1.1 read yes as a boolean, so the same file can mean something else there.

Numbers are the bigger surprise. Unquoted, 007 became 7, 0x10 became 16, 0o14 became 12, 010 became 10 and 1_000 became 1000. A quoted "007" stays "007". A date such as 2025-01-03 stays text, with or without quotes. An unquoted 9007199254740993 came out as 9007199254740992, because a JSON number is exact only up to 9,007,199,254,740,991. Quote long IDs.

These infinity and not-a-number values, .inf, -.inf and .nan, all became null without a warning, since JSON has no such numbers. Every spelling of null we tried, and a key with nothing after the colon, gave null.

Anchors, merge keys and several documents

An alias is replaced by a copy of what it points to. In the sample, copy: *b became the object {"x": 1},. A merge key such as <<: *b pulls in the anchored keys, and a key written next to it wins: with y: 2 beside a merged y: 1, the result held 2. An alias that points back at its own parent cannot be written as JSON, and the page refuses it. The message comes straight from the browser and its line number is blank, so look for the & and * pair yourself.

Several documents separated by --- become one JSON list, and the line under the box says how many it found. Two documents gave a list of two objects.

What is dropped or refused

Comments disappear, because JSON has no place for them. A file that holds only a comment gave null. Tags are refused: !Ref, !!binary, !!set and the code tag !!js/function each gave an error naming the tag, so a CloudFormation template with !Ref will not convert as it is. Duplicate keys are an error on the line of the second one.

Keys that are not text are written as text: 1 became "1", true became "true", and a list used as a key became "a,b".

JSON to YAML

Choose JSON to YAML and the page writes two-space YAML with lists as dashes, no anchors and no line wrapping. Strings that would be read as something else are quoted: "yes", "true", "007", "null" and "1e3" all got single quotes, and so did the keys n and y. The string "2025-01-03" was written without quotes, which is fine here but may be read as a date by a YAML 1.1 tool. Both sample files and the string test converted to YAML and back to JSON with identical data.

Errors and the 10 MB limit

Invalid YAML is reported with the parser's message and a line number: a tab used for indentation was flagged on line 2, a bad indent on line 4. An unclosed bracket on line 1 was reported on line 2, where the parser noticed it, so look one line up. Invalid JSON gets the browser's own message. Files up to 10 MB are accepted: a 2,000,010-byte YAML file took about 3.6 seconds, and the box shows only the first 300 KB while Copy and Download give everything. A 10.6 MB file was turned away.

Related conversions

For XML, use the XML to JSON converter. To flatten the result into rows, send it through JSON to CSV, or check and tidy it first with the JSON formatter.

Measured results

Test runs of the page in headless Chromium with js-yaml 4.1.0. Each cell was read back from the output box.
InputDirectionWhat came out
yes, no, on, off, y, nTo JSONText: "yes", "no", "on", "off", "y", "n"
True and TRUETo JSONtrue for both
007, 0x10, 010, 1_000To JSON7, 16, 10, 1000; quoted "007" stays text
2025-01-03To JSON"2025-01-03", still text
a: 1, ---, b: 2To JSONA list of two objects; the line under the box says 2 documents
tag !Ref MyBucketTo JSONError on line 1: unknown tag
nested.json, 444 bytesTo YAML284 characters; back to JSON gave identical data
2,000,010-byte YAML fileTo JSONConverted in about 3.6 s

When a command-line tool is the better choice

A tool such as yq, or a script in Python or Node.js with a YAML library, is the better fit for files over 10 MB, for YAML with custom tags, or for conversions that run in a pipeline. Use this page to see how a small sample comes out first.

Frequently asked questions

Is my YAML uploaded?

No. The text is converted inside your browser tab. The page fetches the js-yaml library from cdnjs once, and in our log that was one GET without a body. No request contained what we pasted or opened.

Does yes become true?

No. In our test yes, no, on and off stayed text. True, TRUE and true became the boolean true. Quote any value you want kept as text.

Why did 007 become 7?

Unquoted, it is a number in YAML. Put quotes around it and it stays "007". The same applies to 0x10, which became 16.

What happens to comments?

They are dropped, since JSON has no comments.

Can it read several documents in one file?

Yes. Documents separated by --- come out as one JSON list, and the page tells you how many it found.