Read YAML in Java with Jackson

DZone 's Guide to

Read YAML in Java with Jackson

Jackson is one of the best JSON libraries for Java. Now with the YAML extension of Jackson, we can use Jackson to process YAML in Java.

· Java Zone ·
Free Resource

In this post, we will see how we can use Jackson’s YamlFactory to read YAML files into Java Beans.

What is YAML?

YAML is a human friendly data serialization
standard for all programming languages.

YAML is very handy for writing system configuration files. Yaml.org has a list of libraries that you can use to process YAML files in different languages like PHP, Java, Python, Perl, etc.

Jackson is one of the best JSON libraries for Java. Now with the YAML extension of Jackson, we can use Jackson to process YAML in Java. Under the hood, Jackson’s YAML extension uses the SnakeYAML library to parse YAML.

Maven Project Dependencies

We need Jackason databind and Jackson’s YAML extension. These dependencies should get our demo code working:


Reading YAML Files to Java Objects

Let’s consider this sample YAML file for our demo:

# Details of a user
name: Test User
age: 30
  line1: My Address Line 1
  line2: Address line 2
  city: Washington D.C.
  zip: 20000
  - User
  - Editor

Let’s create a User.java Bean to hold our YAML data:

package com.mms.mja.blog.demo.yaml;

import java.util.Map;

public class User {
    private String name;
    private int age;
    private Map<String, String> address;
    private String[] roles;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public int getAge() {
        return age;
    public void setAge(int age) {
        this.age = age;
    public Map<String, String> getAddress() {
        return address;
    public void setAddress(Map<String, String> address) {
        this.address = address;
    public String[] getRoles() {
        return roles;
    public void setRoles(String[] roles) {
        this.roles = roles;

Jackson at Work

Now, here is our code using Jackson’s YamlFactory to read YAML into User bean:

package com.mms.mja.blog.demo.yaml;

import java.io.File;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class YamlTesting {

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        try {
            User user = mapper.readValue(new File("user.yaml"), User.class);
        } catch (Exception e) {
            // TODO Auto-generated catch block

ReflectionToStringBuilder is just a util class to print a Java Object to a String. Here is the output of this program:

  name=Test User
  address={line1=My Address Line 1, line2=Address line 2, city=Washington D.C., zip=20000}

YamlFactory has taken care of mapping address into MAP and roles into a String array. We can also create an Address.java bean class to hold address data and use it instead of MAP in the User bean. YamlFactory will take care of creating the address object and populate it with values from YAML.

jacksonjson, java, yaml

Published at DZone with permission of Manik Magar , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}