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

问题描述 投票:1回答:1

有从数据库检索的模式,我需要转换为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
java mule schema dataweave mule4
1个回答
2
投票
%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}
© www.soinside.com 2019 - 2024. All rights reserved.