Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert from YAML to JSON in Java?

Tags:

java

json

yaml

I just want to convert a string that contains a yaml into another string that contains the corrseponding converted json using Java.

For example supose that I have the content of this yaml

--- paper:    uuid: 8a8cbf60-e067-11e3-8b68-0800200c9a66    name: On formally undecidable propositions of Principia Mathematica and related systems I.    author: Kurt Gödel. tags:    - tag:        uuid: 98fb0d90-e067-11e3-8b68-0800200c9a66        name: Mathematics    - tag:        uuid: 3f25f680-e068-11e3-8b68-0800200c9a66        name: Logic 

in a String called yamlDoc:

String yamlDoc = "---\npaper:\n   uuid: 8a... etc..."; 

I want some method that can convert the yaml String into another String with the corresponding json, i.e. the following code

String yamlDoc = "---\npaper:\n   uuid: 8a... etc..."; String json = convertToJson(yamlDoc); // I want this method System.out.println(json); 

should print:

{     "paper": {         "uuid": "8a8cbf60-e067-11e3-8b68-0800200c9a66",         "name": "On formally undecidable propositions of Principia Mathematica and related systems I.",         "author": "Kurt Gödel."     },     "tags": [         {             "tag": {                 "uuid": "98fb0d90-e067-11e3-8b68-0800200c9a66",                 "name": "Mathematics"             }         },         {             "tag": {                 "uuid": "3f25f680-e068-11e3-8b68-0800200c9a66",                 "name": "Logic"             }         }     ] } 

I want to know if exists something similar to the convertToJson() method in this example.

I tried to achieve this using SnakeYAML, so this code

 Yaml yaml = new Yaml();  Map<String,Object> map = (Map<String, Object>) yaml.load(yamlDoc); 

constructs a map that contain the parsed YAML structure (using nested Maps). Then if there is a parser that can convert a map into a json String it will solve my problem, but I didn't find something like that neither.

Any response will be greatly appreciated.

like image 1000
Miguel A. Carrasco Avatar asked May 19 '14 17:05

Miguel A. Carrasco


People also ask

Can YAML be converted to JSON?

ConvertSimple is another online tool that lets you easily convert YAML to JSON. You just have to copy your YAML entry into the input box on the left, and it will convert it to JSON. The JSON output is in the box to the left.

Can we convert JSONArray to JSONObject?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

Which is better YAML or JSON?

JSON is comparatively faster than YAML. However, if data configurations are small then YAML is better since its interface is much more friendly. JSON has a feature to encode six different data types like an object, array, strings, numbers, null and boolean.

Can we convert list to JSON in Java?

We can convert a list to the JSON array using the JSONArray. toJSONString() method and it is a static method of JSONArray, it will convert a list to JSON text and the result is a JSON array.


1 Answers

Here is an implementation that uses Jackson:

String convertYamlToJson(String yaml) {     ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());     Object obj = yamlReader.readValue(yaml, Object.class);      ObjectMapper jsonWriter = new ObjectMapper();     return jsonWriter.writeValueAsString(obj); } 

Requires:

compile('com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.7.4') 
like image 125
Cory Klein Avatar answered Oct 12 '22 23:10

Cory Klein