Converting YAML to JSON and Vice Versa

YAML (YAML Ain't Markup Language) and JSON (JavaScript Object Notation) are both popular data serialization formats used for configuration files and data exchange. Converting between these two formats can be useful for interoperability between systems that use different data formats. Below, we will explore how to convert YAML to JSON and vice versa using various programming languages and libraries.

1. Converting YAML to JSON

To convert YAML to JSON, you can use libraries available in different programming languages. Below are examples in Python, JavaScript, and Ruby.

1.1. Python Example

In Python, you can use the PyYAML library to parse YAML and the json module to convert it to JSON.

        
import yaml
import json

# Sample YAML data
yaml_data = """
name: John Doe
age: 30
is_active: true
"""

# Convert YAML to JSON
data = yaml.safe_load(yaml_data)
json_data = json.dumps(data, indent=4)
print(json_data)

1.2. JavaScript Example

In JavaScript, you can use the js-yaml library to parse YAML and then convert it to JSON.

        
const yaml = require('js-yaml');

// Sample YAML data
const yamlData = `
name: John Doe
age: 30
is_active: true
`;

// Convert YAML to JSON
const data = yaml.load(yamlData);
const jsonData = JSON.stringify(data, null, 4);
console.log(jsonData);

1.3. Ruby Example

In Ruby, you can use the built-in YAML library to parse YAML and the JSON library to convert it to JSON.

        
require 'yaml'
require 'json'

# Sample YAML data
yaml_data = <<-YAML
name: John Doe
age: 30
is_active: true
YAML

# Convert YAML to JSON
data = YAML.load(yaml_data)
json_data = JSON.pretty_generate(data)
puts json_data

2. Converting JSON to YAML

To convert JSON to YAML, you can similarly use libraries available in different programming languages. Below are examples in Python, JavaScript, and Ruby.

2.1. Python Example

In Python, you can use the json module to parse JSON and the PyYAML library to convert it to YAML.

        
import json
import yaml

# Sample JSON data
json_data = '''
{
"name": "John Doe",
"age": 30,
"is_active": true
}
'''

# Convert JSON to YAML
data = json.loads(json_data)
yaml_data = yaml.dump(data, default_flow_style=False)
print(yaml_data)

2.2. JavaScript Example

In JavaScript, you can use the js-yaml library to convert JSON to YAML.

        
const yaml = require('js-yaml');

// Sample JSON data
const jsonData = {
name: "John Doe",
age: 30,
is_active: true
};

// Convert JSON to YAML
const yamlData = yaml.dump(jsonData);
console.log(yamlData);

2.3. Ruby Example

In Ruby, you can use the JSON library to parse JSON and the YAML library to convert it to YAML.

        
require 'json'
require 'yaml'

# Sample JSON data
json_data = '{
"name": "John Doe",
"age": 30,
"is_active": true
}'

# Convert JSON to YAML
data = JSON.parse(json_data)
yaml_data = YAML.dump(data)
puts yaml_data

3. Conclusion

Converting between YAML and JSON is straightforward with the right libraries in various programming languages. This capability allows developers to work with different data formats as needed, enhancing interoperability and flexibility in applications.