This tutorial is about YAML parser using Java, read and write YAML content with examples.

How to read/write YAML file in Java

There are several parser libraries available to parse

  • Jackson
  • SnakeYAML library

This post is about an example of reading and writing a yaml file using jackson-dataformat-yaml - Jackson library. Jackson is a popular library in Java used to convert json/XML to/from Java objects.

Sample Yaml file example

yaml is a superset of json. It contains key and value pairs with included indentation and tabs.

Given the yaml example file with database dictionary details.

--- # Application configuration - application.yaml
author: Franc
database:
        driver: com.mysql.jdbc.Driver
        port: 3306
        dbname: mydb
        username: root
        password:

Add Jackson dependency to Maven

Add the following dependencies in pom.xml for maven-related projects

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.10.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.3</version>
</dependency>

add the following tendencies Gradle file for projects using the Gradle build tool.

compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml',
version: '2.10.3'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind',
version: '2.10.3'

jackson-databind is a library for data binding core API which every format implementation library extends - jackson-dataformat-yaml.

How to read a yaml file in Java?

yaml can be read as an object or map in Java.

using the jackson-databind module, We can use yamlfactory which parses the physical path of a file pass it into ObjectMapper, and maps to the same class.

Let’s Create a basic object with a setter and getter with the same hierarchy as follows

if yaml contains scalar types, create a setter and getter for scalar types if contains an array of dictionaries, create an internal child class for it

package org.w3schools.io.yaml;
public class ApplicationConfig {
    public ApplicationConfig() {
    }
    private String author;
    private DatabaseConfig database;
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public DatabaseConfig getDatabase() {
        return database;
    }
    public void setDatabase(DatabaseConfig database) {
        this.database = database;
    }
    @Override
    public String toString() {
        return "\nAuthor: " + author
        + "\ndatabase: " + database + "\n";
    }
}

POJO object declared for database dictionary with setter and getters

package org.w3schools.io.yaml;
public class DatabaseConfig {
    private String driver;
    private Integer port;
    private String dbname;
    private String username;
    private String password;
    public String getDriver() {
        return driver;
    }
    public void setDriver(String driver) {
        this.driver = driver;
    }
    public Integer getPort() {
        return port;
    }
    public void setPort(Integer port) {
        this.port = port;
    }
    public String getDbname() {
        return dbname;
    }
    public void setDbname(String dbname) {
        this.dbname = dbname;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "\nDriver: " + driver + "\nport: "
        + port + "\ndbname: " + dbname
        + "\nusername: " + username
        + "\npassword: " + password + "\n";
    }

}
package org.w3schools.io.yaml;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class YamlReaderExample {
    public static void main(String[] args) throws Exception {
        File file = new File("src/main/resources/application.yaml");
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        ApplicationConfig config = objectMapper.readValue(file, ApplicationConfig.class);
        System.out.println("Application config info " + config.toString());
    }
}

Write Object to YAML in Java

Data is in the java POJO object, Create an object mapper with the yaml implementation class - YAMLFactory writeValue method creates a yaml file using a POJO object.

Finally, POJO data is serialized into YAML format.

package org.w3schools.io.yaml;

import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class YamlReaderExample {
    public static void main(String[] args) throws Exception {
        DatabaseConfig dbconfig = new DatabaseConfig();
        dbconfig.setDbname("newdb");
        dbconfig.setDriver("newdriver");
        dbconfig.setPort(6601);
        dbconfig.setUsername("appuser");
        dbconfig.setPassword("apppassword");
        ApplicationConfig appConfig = new ApplicationConfig();
        appConfig.setAuthor("Tom");
        appConfig.setDatabase(dbconfig);
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        objectMapper.writeValue(new File("src/main/resources/application1.yaml"), appConfig);
    }
}

And the application1.yaml file created with the below content

author: "Tom"
database:
  driver: "newdriver"
  port: 6601
  dbname: "newdb"
  username: "appuser"
  password: "apppassword"