Java – How to convert JSON to YAML using ObjectMapper ?

In this article, we will discuss how to convert JSON file to YAML file using ObjectMapper

1. Required libraries :

1.1 Maven Co-ordinates :

<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-yaml</artifactId>
			<version>2.13.4</version>
</dependency>

1.2 JAR files to download :

Download below jar files and include them in Project classpath

1.3 Project class-path :

2. JSON to YAML file conversion :

In the below illustration, we will convert JSON to YAML file,

  • 1st step is to read the contents of the JSON file to a String
  • 2nd step is to convert String into JSON node using readTree() method of ObjectMapper class by passing JSON contents in String format
  • 3rd step is to convert JsonNode into YAML file using writeValueAsString() method of YamlMapper class by passing JsonNode as inputargument

JSON file contents – Person.json :

{
  "title" : "Mr.",
  "firstName" : "Sam",
  "lastName" : "Anton",
  "address" : {
    "flatNumber" : "BV-1025",
    "buildingName" : "Shivaji",
    "plotNumber" : "1093",
    "sector" : "Sector 19",
    "city" : "Bengaluru",
    "state" : "Karnataka",
    "country" : "India",
    "nodeName" : "South Bengaluru"
  },
  "items" : [ {
    "itemName" : "Television",
    "itemBoughtYear" : "2014"
  }, {
    "itemName" : "Washing Machine",
    "itemBoughtYear" : "2020"
  }, {
    "itemName" : "Refrigerator",
    "itemBoughtYear" : "2011"
  }, {
    "itemName" : "Grinder",
    "itemBoughtYear" : "2012"
  }, {
    "itemName" : "Computer",
    "itemBoughtYear" : "2010"
  } ]
}

JsonToYamlConversion.java

package in.bench.resources.yaml.conversion;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

public class JsonToYamlConversion {

	public static void main(String[] args) {

		try {
			// 1st step - read JSON contents
			String jsonContent = new String(Files.readAllBytes(Paths.get(
					"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\Person.json")));


			// 2nd step - parse JSON contents
			JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonContent);


			// 3rd step - convert JSON to YAML
			String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);


			// print to console
			System.out.println(jsonAsYaml);

		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output in console :

---
title: "Mr."
firstName: "Sam"
lastName: "Anton"
address:
  flatNumber: "BV-1025"
  buildingName: "Shivaji"
  plotNumber: "1093"
  sector: "Sector 19"
  city: "Bengaluru"
  state: "Karnataka"
  country: "India"
  nodeName: "South Bengaluru"
items:
- itemName: "Television"
  itemBoughtYear: "2014"
- itemName: "Washing Machine"
  itemBoughtYear: "2020"
- itemName: "Refrigerator"
  itemBoughtYear: "2011"
- itemName: "Grinder"
  itemBoughtYear: "2012"
- itemName: "Computer"
  itemBoughtYear: "2010"

In one of the previous article, already discussed YAML file to JSON conversion in detail

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java – How to convert XML to JSON using JSONObject ?
Java – How to convert YAML to JSON using ObjectMapper ?