【问题标题】:Java - JSON to YAML (Dynamic) [duplicate]Java - JSON 到 YAML(动态)[重复]
【发布时间】:2021-02-11 06:02:05
【问题描述】:

我想将 JSON 转换为 YAML。我想在Java中动态地做到这一点。基于componentId,属性会改变。这个 JSON 可以变化并支持多个 componentId,这来自 HTTP 请求。

示例 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"
        }
    }
]

预期的 YAML:

- from:
    uri: "timer:tick"
    parameters:
      period: "5000"
    steps:
      - set-body:
          constant: "Hello Yaml!!!"
      - transform:
          simple: "${body.toUpperCase()}"
      - to: "log:info”

【问题讨论】:

标签: java json yaml


【解决方案1】:

我认为您应该能够使用 Jackson 将任何类型的 JSON 转换为 YAML。将以下依赖项添加到您的项目中:

        <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>

如果您有这样的输入 Kamelet JSON 字符串:

{
  "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}"
            }
          }
        ]
      }
    }
  }
}

您可以像这样使用 Jackson 轻松地将其转换为 YAML:

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,但我的 JSON 不同。基于那个json,我需要生成YAML
  • 如果您能用 JSON 的详细信息以及哪些属性触发 YAML 生成的差异来更新您的问题,我们将不胜感激
  • Rohan,用完整的 JSON 更新了我的问题
猜你喜欢
  • 2021-03-20
  • 2012-02-19
  • 2019-01-25
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多