mule4 / java преобразование схемы Sql ddl в схему json

#java #mule #schema #dataweave #mule4

#java #mule #схема #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
 

Комментарии:

1. Что вы пробовали до сих пор?

Ответ №1:

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