Convert JSON to YAML Online

(and in every language)

JSON

Paste your JSON here

YML

See the YML output here


Converting JSON to YAML is a two part process.

First you have to convert a string of JSON into an object in whichever language you're using. This process is referred to as to parse or to deserialize.

Next you'll need to convert that object into a YAML string. This process is referred to as to emit, to serialize, or to stringify.

Technically speaking, JSON already is YAML since YAML allows for the use of brackets and commas in the exact same way that JSON does. However, for the sake of human-readability, it's very convenient to use the whitespace syntax of YAML without any brackets.

Bash

npm install -g json2yaml
# From file
json2yaml example.json

# From stdin
cat example.json | json2yaml

Haskell

import           Data.Aeson (decode)
import           System.Environment (getArgs)
import           Control.Monad (forM_)
import           Control.Applicative ((<$>))
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Data.Yaml as Y

main :: IO ()
main = do
  args <- getArgs
  forM_ args $ \arg -> do  -- args is a list of JSON files
    j <- decode <$> BLC.readFile arg
    case j of 
      Nothing -> error "Invalid JSON file"
      Just v -> Y.encodeFile (arg ++ ".yaml") (v :: Y.Value)

JavaScript

(function () {
  "use strict";
  
  var YAML = require('json2yaml')
    , json
    , data
    , yml
    ;
    
  json = '{ "foo": "bar" }';
  data = JSON.parse(json);
  yml = YAML.stringify(data);
  
  console.log(yml);
}());

JavaScript (browser only)

<script src="http://jsontoyaml.com/js/json2yaml.js"></script>
(function () {
  "use strict";
  
  var YAML = window.YAML
    , json
    , data
    , yml
    ;
    
  json = '{ "foo": "bar" }';
  data = JSON.parse(json);
  yml = YAML.stringify(data);
  
  console.log(yml);
}());

Perl

use JSON;
use YAML::XS;

my $json = <<'...';
{
  "foo": "bar"
}
...

print Dump decode_json $json;

Python

import json
import yaml

str = '{ "foo": "bar" }'
data = json.loads(str)
yml = yaml.safe_dump(data)

print(yml)

Ruby

require 'json'
require 'yaml'

json = '{ "foo": "bar" }'
data = JSON.parse(json)
yml = YAML::dump(data)

puts yml

Other languages...

Want to see to to convert JSON to YAML in your favorite language?

Please request an example or add it yourself (you can use examples/ruby.jade as a template).