How to read a Yaml File and write it in indentation or Json format with SnakeYaml ?

Yaml Representation


SnakeYaml 1) is a java library that manages yaml document.


Object to read or write must be:

  • a java bean (ie getter and setter + public properties)
  • a LinkedList
  • a HashMap

How to Read a Yaml File


Log file:

Time: 2001-11-23 15:01:42 -5
User: ed
  This is an error message
  for the log file
Time: 2001-11-23 15:02:31 -5
User: ed
  A slightly different error
Date: 2001-11-23 15:03:17 -5
User: ed
  Unknown variable "bar"
  - file:
    line: 23
    code: |
      x = MoreObject("345\n")
  - file:
    line: 58
    code: |-
      foo = bar

With SnakeYaml, the data object are LinkedList.

  • Dependency:
  • Code
public void testLoadManyDocuments() throws FileNotFoundException {
    InputStream input = new FileInputStream(new File(
    Yaml yaml = new Yaml();
    for (Object data : yaml.loadAll(input)) {
{Time=Fri Nov 23 21:01:42 CET 2001, User=ed, Warning=This is an error message for the log file}
{Time=Fri Nov 23 21:02:31 CET 2001, User=ed, Warning=A slightly different error message.}
{Date=Fri Nov 23 21:03:17 CET 2001, User=ed, Fatal=Unknown variable "bar", Stack=[{, line=23, code=x = MoreObject("345\n")
}, {, line=58, code=foo = bar}]}


  • curly brace { } are indicating a map
  • and [] indicates a sequence


Built with the dumper option to choose the format and styling.

How to write in a Indentation Block format

DumperOptions options = new DumperOptions();
Yaml yaml = new Yaml(options);
  • Then dump your object
StringWriter stringWriter = new StringWriter();
yaml.dump(object, stringWriter);

How to write in a extended JSON format

The flow style 3) of Yaml is an extended Json format. It means that it adds features such as:

  • tag for data type definition
  • the possibility to tweak the quotation around the value

The below code shows you how you can simulate a valid json format.

  • Instantiate the dump formatting options
DumperOptions options = new DumperOptions();
  • Flow is the JSON format extended 4)
  • No line break (Not json compatible)
  • Quote parameter 5)
  • To delete the yaml implicit tag such as the tag !!timestamp for timestamp 6) create the following resolver class.
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.resolver.Resolver;

public class YamlNoImplicitTagResolver extends Resolver {

   * do not resolve float, timestamp, ...
  protected void addImplicitResolvers() {
    // addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
    // addImplicitResolver(Tags.FLOAT, FLOAT, "-+0123456789.");
    // addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789");
    // addImplicitResolver(Tag.INT, INT, "-+0123456789");
    // addImplicitResolver(Tag.MERGE, MERGE, "<");
    // addImplicitResolver(Tag.NULL, NULL, "~nN\0");
    // addImplicitResolver(Tag.NULL, EMPTY, null);

  • Instantiate snakeyaml
YamlNoImplicitTagResolver noImplicitTagResolver = new YamlNoImplicitTagResolver();
Yaml yaml = new Yaml(
        new Constructor(),
        new Representer(),
  • Then dump your object
StringWriter stringWriter = new StringWriter();
yaml.dump(object, stringWriter);

Discover More
Yaml Representation
YAML Ain’t Markup Language (YAML)

YAML minimizes the amount of structural characters in order to view and understand more easily the data. For example: indentation may be used for structure, colons separate key: value pairs, ...

Share this page:
Follow us:
Task Runner