Convert YAML to JSON Online

(and in every language)

YML

Paste your YAML here

JSON

See the JSON output here


Converting YAML to JSON is a two part process.

First you have to convert a string of YML 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 JSON string. This process is referred to as to emit, to serialize, or to stringify.

Technically speaking, any YAML parser should have the option to stringify in JSON (basically the "compressed" version of YAML - no optional whitespace or comments, but all optional quotes). However, since YAML is a superset of JSON, using a JSON stringifier will ensure that none of the YAML-only features (lack of quotes or inclusion of comments) makes it into the output.

Bash

npm install -g yaml2json
# From file
yaml2json example.yml

# From stdin
cat example.yml | yaml2json

JavaScript

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

JavaScript (browser only)

<script src="http://yamltojson.com/js/yaml.js"></script>
(function () {
  "use strict";
  
  var YAML = window.YAML
    , json
    , data
    , yml
    ;
    
  yml = '---\n  foo: bar';
  data = YAML.parse(yml);
  json = JSON.stringify(data);
  
  console.log(json);
}());
  • Download yaml.js
  • JSON.stringify
  • YAML.parse
  • Note: where size is a concern another YAML library which is not as complete, but handles the most common use cases may be a more suitable option.

Ruby

require 'json'
require 'yaml'

yml = <<-eos
---
  foo: bar
eos

data = YAML::load(yml)
json = JSON.dump(data)

puts json

Python

import yaml
import json

yml = """
---
  foo: bar
"""
data = yaml.load(yml)
json = json.dumps(data)

print(json)

Perl

use JSON;
use YAML::XS;

my $yaml = <<'...';
---
foo: bar
...

print encode_json Load $yaml;

Java

String yamlString = "---\n  foo: bar";

Yaml yaml= new Yaml();
Map map= (Map) yaml.load(yamlString);

JSONObject jsonObject=new JSONObject(map);
System.out.println(jsonObject.toString());

Other languages...

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

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