java - mule4/java 将 Sql ddl 模式转换为 json 模式

标签 java mule schema dataweave mule4

有从数据库检索模式,我需要将其转换为 json 模式,有人可以让我知道如何在 java 或 mule 中执行此操作。

下面是我的代码:

{"Schema": [
        {
            "Column_Name": "Employee Name",
            "Type": "varchar",
            "SafeType": "string",
            "Length": 51,
            "Description": null
        },
        {
            "Column_Name": "Username",
            "Type": "varchar",
            "SafeType": "string",
            "Length": 51
            }
]}

Output should be:

{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "properties": {
        "Employee Name": {
            "$id": "#/properties/Employee Name",
            "type": "varchar",
            "maxLength":50
        },
        "Username": {
            "$id": "#/properties/Username",
            "type": "string",
            "maxLength":50
        }
    }
}

Please guide

最佳答案

%dw 2.0
output application/json

var rootObj = {
  "definitions": {},
  "\$schema":    "http://json-schema.org/draft-07/schema#",
  "\$id":        "http://example.com/root.json",
  "type":        "object",
  "title":       "The Root Schema"
}

var props = payload.Schema reduce ((schema, acc={}) ->
  acc ++ {
    (schema.Column_Name): {
      "\$id":      "#/properties/$(schema.Column_Name)",
      "type":      schema.Type,
      "maxLength": schema.Length - 1
    }
  })
---
rootObj ++ {"properties": props}

关于java - mule4/java 将 Sql ddl 模式转换为 json 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55762896/

相关文章:

tomcat - 将多个应用程序部署到 mule tomcat 服务器

clojure - 如何验证 malli 模式中的本地日期?

java - 关系存储和多维数据集检查/反射

java - 使用 Date(milliseconds) 构造函数初始化日期

java - 让线程 A 阻塞 B & C,反之亦然,但不让线程 B 阻塞线程 C,反之亦然?

mule - Apikit - 状态码最佳实践

xml - Dataweave - 循环深度映射 XML

database - 直接引用 Oracle 中的公共(public)同义词

java - Java HashMap 中的非通用错误

java - LinkedList sum 函数完成后会删除整个列表吗?