需要将CSV格式固定为JSON转换的CSV建议。我的JSON如下所示:
请建议使用任何python库/或相关线程。
{
"database": "test",
"schema": "test",
"table": "order_info",
"description": "Each order generated on system can be tracked here with key information related to customers, merchant and Cost of the order",
"frequency": "120",
"columns": [
{
"name": "id",
"type": "integer",
"description": "Primary key of the table, refers each unique order placed by customer",
"is_nullable": false,
"logic": null,
"comments": ""
},
{
"name": "creation_ts",
"type": "timestamp without time zone",
"description": "Order Creation Time (UTC)",
"is_nullable": false,
"logic": null,
"comments": ""
},
{
"name": "update_ts",
"type": "timestamp without time zone",
"description": "Order Updation time (UTC)",
"is_nullable": false,
"logic": null,
"comments": ""
}
]
}
您可以非常方便地使用熊猫,首先将json字符串加载到字典中,然后在该字典上使用数据框来获取所需的内容。在代码中:
import pandas as pd
import json
json_string = """{
"database": "test",
"schema": "test",
"table": "order_info",
"description": "Each order generated on system can be tracked here with key information related to customers, merchant and Cost of the order",
"frequency": "120",
"columns": [
{
"name": "id",
"type": "integer",
"description": "Primary key of the table, refers each unique order placed by customer",
"is_nullable": false,
"logic": null,
"comments": ""
},
{
"name": "creation_ts",
"type": "timestamp without time zone",
"description": "Order Creation Time (UTC)",
"is_nullable": false,
"logic": null,
"comments": ""
},
{
"name": "update_ts",
"type": "timestamp without time zone",
"description": "Order Updation time (UTC)",
"is_nullable": false,
"logic": null,
"comments": ""
}
]
}"""
parsed = json.loads(json_string)
df = pd.DataFrame(parsed["columns"])
[如果您想做相反的事情,只需将csv加载到DataFrame中并执行df.to_json()