0

I have a requirement where i need to parse a DDL from Oracle table and generate a JSON data out of it. For that i had used DDL parser in Python to convert it to string however i couldnt able to get it as JSON Data as such.

import json

from ddlparse import DdlParse

sample_ddl = """
CREATE TABLE SCOTT.EMPLOYEE (   
EMP_NO VARCHAR2(5 CHAR) NOT NULL , 
    EMP_NAME VARCHAR2(50 CHAR) NOT NULL , 
    CONSTRAINT EMP_PK PRIMARY KEY (EMP_NO)
);
"""

# parse pattern (1-2) : Specify source database
table = DdlParse().parse(ddl=sample_ddl, source_database=DdlParse.DATABASE.oracle)


print("* COLUMN *")
for col in table.columns.values():
     
    col_info = {}

    #col_info["table:name"]            = table.name
    col_info["name"]                  = col.name
    col_info["data_type"]             = col.data_type
    col_info["length"]                = col.length
    col_info["precision(=length)"]    = col.precision
    col_info["scale"]                 = col.scale
    col_info["not_null"]              = col.not_null
    col_info["PK"]                    = col.primary_key
    col_info["bigquery_field"]        = json.loads(col.to_bigquery_field())   

    print(json.dumps(col_info, indent=2, ensure_ascii=False))

For the above code the Output is coming as string but not as a JSON data as it should look below. I need the out in JSON Data format as

  {
    "name": "EMP",
    "columns":
    [
        {
            "name": "EMP_NO",
            "dataType": "VARCHAR2(5 CHAR) ",
            "nullable": false
        },
        {
            "name": "EMP_NAME",
            "dataType": "VARCHAR2(50 CHAR)",
            "nullable": false
        }
    ],
    "primaryKey":
    [
        "EMP_NO"
    ]
}

Any help will be highly appreciated.

Thanks

Amdone
  • 5
  • 4
  • What does *is coming as string but not as a JSON data* mean? JSON is a string representation of the data: [JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format](https://www.rfc-editor.org/rfc/rfc8259) – astentx Mar 01 '23 at 14:39
  • Thanks for your reply! Basically I want to format the string to JSON as shown in the output which I can’t able to do ! – Amdone Mar 01 '23 at 16:12
  • Please show your current output and add an information how do you know if json is not pretty-printed – astentx Mar 01 '23 at 16:38
  • Current output is like this `{ "name": "EMP_NO", "data_type": "VARCHAR2", "length": 5, "precision(=length)": 5, "scale": null, "not_null": true, "PK": true, "bigquery_field": { "name": "EMP_NO", "type": "STRING", "mode": "REQUIRED" } } { "name": "EMP_NAME", "data_type": "DATE", "length": null, "precision(=length)": null, "scale": null, "not_null": true, "PK": false, "bigquery_field": { "name": "EMP_NAME", "type": "DATETIME", "mode": "REQUIRED" } }` – Amdone Mar 01 '23 at 16:46

0 Answers0