1. Introduction

JavaScript Object Notation (JSON) and Yet Another Markup Language (YAML) are two popular data serialization languages used to represent data structures and values. Both enable data storage, transfer, and distribution, often for use between computer systems.

In this tutorial, we’ll discuss different ways to convert a JSON file to a YAML file and vice versa.

2. The Importance of JSON-to-YAML Conversion

JSON is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate. It’s an alternative to XML and primarily transmits data between servers and a web application. However, it can also be used to store data in files or to send data between different applications.

On the other hand, YAML is a human-readable data serialization language that is commonly used for configuration files and in applications where data is being stored or transmitted.

YAML is a superset of JSON, meaning that all valid JSON files are also valid YAML files. However, YAML has a number of features that make it more powerful and flexible than JSON.

Let’s see some JSON features:

  • primarily transmits data between a server and a web application
  • represents structured data
  • often used for configuration files, data storage, and data exchange between systems
  • key-value pair structure
  • supports various data types
  • versatile and suitable for a wide range of applications

Similarly, let’s explore some key features of YAML:

  • human-readable data serialization format
  • designed to be expressive and easy to read
  • popular choice for configuration files and data representation in settings where human interaction is expected
  • uses indentation to represent the structure of data
  • relies on a minimal set of punctuation characters

The main need to convert data between JSON and YAML arises from the diverse environments in which these formats are used. Understanding how to perform these conversions is crucial for us as developers, system administrators, and for anyone working with configuration files or data interchange.

3. Converting JSON to YAML

Transforming JSON data into YAML is a common task, particularly for configuration files and data representation.

Let’s use an example JSON file, input.json, to illustrate the conversion process. We can view its contents using the cat command:

$ cat input.json
{
  "name": "John Doe",
  "age": 25,
  "address": {
    "street": "123 Main Street",
    "city": "New York",
    "state": "NY",
    "zip": "10001"
  },
  "hobbies": [
    "reading",
    "writing",
    "coding"
  ]
}

The JSON object is a representation of a person named John Doe, who is 25 years old and lives at 123 Main Street, New York, NY 10001. John’s hobbies are reading, writing, and coding.

3.1. Using yq to Convert JSON to YAML

One of the easiest ways to convert a JSON file to a YAML file is to use the yq command-line tool.

Before we get started, we may need to install yq on our system. On a Debian or Ubuntu-based system, we can install yq using apt:

sudo apt update
sudo apt install yq

Also, on a Red Hat or CentOS-based system, we can install yq using yum:

sudo yum install yq

yq is a wrapper around the jq tool that enables us to manipulate JSON and YAML files using jq syntax:

$ yq -p json -o yaml input.json
name: John Doe
age: 25
address:
  street: 123 Main Street
  city: New York
  state: NY
  zip: "10001"
hobbies:
  - reading
  - writing
  - coding

Here, yq -p json -o yaml input.json converts the input.json file to YAML using the yq utility. The -p option specifies the input format as JSON, and the -o option specifies the output format as YAML.

The resulting output shows an equivalent YAML representation of the JSON file.

3.2. Using Python to Convert JSON to YAML

Another way to convert a JSON file to a YAML file is to use the Python programming language.

Python has built-in modules for handling JSON and YAML data, namely json and yaml:

$ python3 -c 'import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))' < input.json

address:
  city: New York
  state: NY
  street: 123 Main Street
  zip: '10001'
age: 25
hobbies:
- reading
- writing
- coding
name: John Doe

Here, python3 -c ‘import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))’ < input.json converts the input.json file into a YAML file while executing the Python code given as an argument with the -c option.

First, the code imports the sys, yaml, and json modules. Afterward, the code reads and parses the standard input (sys.stdin) as a JSON document using json.loads. Then, the result is converted into a YAML string using yaml.dump and printed to the standard output.

Importantly, < input.json redirects the content of the input.json file to the standard input of the command. This way, we can read and convert the JSON file without creating a separate Python script.

3.3. Using Ruby to Convert JSON to YAML

Similarly, Ruby offers the capability to convert JSON to YAML using its built-in modules:

$ ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))' < input.json
---
name: John Doe
age: 25
address:
  street: 123 Main Street
  city: New York
  state: NY
  zip: '10001'
hobbies:
- reading
- writing
- coding

In this example, the command ruby -ryaml -rjson -e ‘puts YAML.dump(JSON.parse(STDIN.read))’ < input.json converts the input.json file into a YAML file. This command requires the yaml and json libraries before executing the code with the -r option.

Then, the code reads and parses the standard input (STDIN) as a JSON document (JSON.parse). Consequently, it converts the result into a YAML string (YAML.dump) and prints it to the standard output (puts) with the -e option.

4. Converting YAML to JSON

Converting YAML to JSON is equally important, especially when dealing with systems or tools that expect JSON data. We’ve various methods to perform this conversion.

For instance, let’s use an example YAML file, input.yml:

$ cat input.yml
name: John Doe
age: 25
address:
  street: 123 Main Street
  city: New York
  state: NY
  zip: "10001"
hobbies:
  - reading
  - writing
  - coding

This is a YAML representation of a person named John Doe, who is 25 years old and lives at 123 Main Street, New York, NY 10001. John‘s hobbies are reading, writing, and coding.

4.1. Using yq to Convert YAML to JSON

yq is a multifunctional tool that also supports converting YAML to JSON:

$ yq -p yaml -o json input.yml
{
  "name": "John Doe",
  "age": 25,
  "address": {
    "street": "123 Main Street",
    "city": "New York",
    "state": "NY",
    "zip": "10001"
  },
  "hobbies": [
    "reading",
    "writing",
    "coding"
  ]
}

Here, the command yq -p yaml -o json input.yml converts the YAML file input.yml to JSON format using the yq utility. The -p option specifies that the input is in YAML format, and the -o option specifies that the output should be in JSON format. The resulting output is the equivalent JSON representation in YAML format.

4.2. Using Python to Convert YAML to JSON

Also, Python provides a straightforward way to convert YAML to JSON.

We can utilize Python’s built-in modules for the task:

$ python3 -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin)))' < input.yml

{
  "name": "John Doe",
  "age": 25,
  "address": {
    "street": "123 Main Street",
    "city": "New York",
    "state": "NY",
    "zip": "10001"
  },
  "hobbies": [
    "reading",
    "writing",
    "coding"
  ]
}

In this command, we use the -c option with python3 to import the sys, yaml, and json modules. We then read and parse the standard input (sys.stdin) as a YAML document (yaml.safe_load). Next, we convert the result into a JSON string (json.dumps) and print out the standard output.

4.3. Using Ruby to Convert YAML to JSON

Additionally, Ruby, with its built-in modules, offers a method to convert YAML to JSON:

$ ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(STDIN))' < input.yml
{
  "name": "John Doe",
  "age": 25,
  "address": {
    "street": "123 Main Street",
    "city": "New York",
    "state": "NY",
    "zip": "10001"
  },
  "hobbies": [
    "reading",
    "writing",
    "coding"
  ]
}

Similar to our conversion from JSON to YAML with Ruby, the code reads and parses the standard input (STDIN) as a YAML document (YAML.load). Then, it converts the result into a JSON string with indentation and line breaks (JSON.pretty_generate) and prints it to the standard output (puts) with the -e option.

5. Conclusion

In this article, we’ve explored various methods for converting between JSON and YAML and vice versa.

This process ensures interoperability, ease of configuration, and the flexibility to transform and manipulate data as needed. Whether by using command-line tools like yq or programming languages like Perl, Python, and Ruby, we now have the means to efficiently handle JSON and YAML conversions in our work.

Comments are closed on this article!