How to read/write YAML file in java

There are many parser libraries available to parse

  • Jackson
  • SnakeYAML library

This post is an example for reading and writing 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 dependencies are added to 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.

Parse/Read yaml file into an object in java

Create a basic object with setter and getter as follows

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

Data is in java POJO object, Create an object mapper with yaml implementation class - YAMLFactory writeValue method creates yaml file using 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 —yaml author: “Tom” database: driver: “newdriver” port: 6601 dbname: “newdb” username: “appuser” password: “apppassword”