1

I want to convert JSON to YAML. And I want to this in Java dynamically. Based on the componentId, properties will change. This JSON can vary and supports multiple componentIds and this comes from HTTP request.

Example JSON:

[
    {
        "id": "timer",
        "attributes": {
            "type": "tick"
            "period": "5000"
        },
        "output": "transform"
    },
    {
        "id": "transform",
        "attributes": {
            "expression": "${body.toUpperCase()}”,
            “type”: “simple"
        },
        "output": "log"
    },
    {
        "id": "log",
        "attributes": {
            "message": "hello world”,
            “type”: “info"
        }
    }
]

Expected YAML:

- from:
    uri: "timer:tick"
    parameters:
      period: "5000"
    steps:
      - set-body:
          constant: "Hello Yaml!!!"
      - transform:
          simple: "${body.toUpperCase()}"
      - to: "log:info”
Vasu Youth
  • 323
  • 3
  • 13
  • 5
    Does this answer your question? [Convert JSON to YAML. Parsing JSON to YAML](https://stackoverflow.com/questions/26235350/convert-json-to-yaml-parsing-json-to-yaml) – SRJ Feb 11 '21 at 06:05
  • Could you please share full JSON? – Rohan Kumar Feb 11 '21 at 07:36

1 Answers1

1

I think you should be able to convert any kind of JSON to YAML using Jackson. Add the following dependencies to your project:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>${jackson.version}</version>
        </dependency>

If you have an input Kamelet JSON string like this:

{
  "apiVersion": "camel.apache.org/v1alpha1",
  "kind": "Kamelet",
  "metadata": {
    "name": "echo-sink",
    "labels": {
      "camel.apache.org/kamelet.type": "sink"
    }
  },
  "spec": {
    "definition": {
      "title": "Echo",
      "description": "Replies with an echo message to each incoming event",
      "properties": {
        "prefix": {
          "title": "Prefix",
          "description": "The prefix to prepend to the incoming event",
          "type": "string",
          "default": "echo: "
        }
      }
    },
    "types": {
      "in": {
        "mediaType": "text/plain"
      },
      "out": {
        "mediaType": "text/plain"
      }
    },
    "flow": {
      "from": {
        "uri": "kamelet:source",
        "steps": [
          {
            "set-body": {
              "simple": "{{prefix}}${body}"
            }
          }
        ]
      }
    }
  }
}

You can easily convert it into YAML using Jackson like this:

File jsonFile = new File(JsonToYaml.class.getResource("/kamelet.json").getFile());

ObjectMapper jsonMapper = new ObjectMapper();
YAMLMapper yamlMapper = new YAMLMapper();

// Read file as JsonNode
JsonNode jsonNode = jsonMapper.readTree(jsonFile);
// Convert it into YAML String
String yamlOutput = yamlMapper.writeValueAsString(jsonNode);

System.out.println(yamlOutput);
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40