1You can convert JSON to YAML with two lines of code in Jackson:
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.core.JsonProcessingException;
6import com.fasterxml.jackson.databind.JsonNode;
7import com.fasterxml.jackson.databind.ObjectMapper;
8import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
9
10public class Library {
11
12 public String asYaml(String jsonString) throws JsonProcessingException, IOException {
13 // parse JSON
14 JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
15 // save it as YAML
16 String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
17 return jsonAsYaml;
18 }
19
20}
21You will need to add dependencies to Jackson Core, DataBind and DataFormat YAML. Below is a snippet for Gradle:
22
23compile 'com.fasterxml.jackson.core:jackson-core:2.8.6'
24compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6'
25compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.6