有没有办法将下面嵌套的 json 数据插入 sql 服务器
Is there any way to insert below nested json data into sql server
我正在处理嵌套的 JSON 数据,我需要将其加载到 SQL SERVER 2012。嵌套的 JSON 包含两个根,即一列和另一行。我需要将行中的值放入列中。请看下面的结构:
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "timestamp",
"type": "datetime"
},
{
"name": "id",
"type": "string"
},
{
"name": "name",
"type": "string"
},
{
"name": "url",
"type": "string"
},
{
"name": "duration",
"type": "real"
}
],
"rows": [
[
"2019-04-08T13:09:52.871Z",
"244",
"Internal",
"https://google.com",
1245,
]
]
}
]
}
结果:
timestamp id name url duration
2019-04-08 244 Internal https://google.com 1245
这里,在 sql 服务器中,它应该从列中获取列名,从行中获取每列的值
假设您将 json 存储到名为 json.txt
的文件中
import json
with open('json.txt') as f:
data = json.load(f)
tableName = data['tables'][0]["name"]
sqlCreateTable = 'CREATE TABLE ' + tableName + ' (\n'
sqlInsertInto = 'INSERT INTO ' + tableName + ' ('
for i in range(0,len(data['tables'][0]['columns'])):
columnName = data['tables'][0]['columns'][i]['name']
type = data['tables'][0]['columns'][i]['type']
sqlCreateTable += columnName + " " + type + ',\n'
sqlInsertInto += columnName + ', '
sqlCreateTable = sqlCreateTable[:-2] + '\n);'
sqlInsertInto = sqlInsertInto[:-2] + ')\nVALUES ('
for value in data['tables'][0]['rows'][0]:
sqlInsertInto += str(value) + ', '
sqlInsertInto = sqlInsertInto[:-2] + ');'
print(sqlCreateTable)
print(sqlInsertInto)
创建 table 的输出:
CREATE TABLE PrimaryResult (
timestamp datetime,
id string,
name string,
url string,
duration real
);
插入 table 的输出:
INSERT INTO PrimaryResult (timestamp, id, name, url, duration)
VALUES (2019-04-08T13:09:52.871Z, 244, Internal, https://google.com, 1245);
我正在处理嵌套的 JSON 数据,我需要将其加载到 SQL SERVER 2012。嵌套的 JSON 包含两个根,即一列和另一行。我需要将行中的值放入列中。请看下面的结构:
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "timestamp",
"type": "datetime"
},
{
"name": "id",
"type": "string"
},
{
"name": "name",
"type": "string"
},
{
"name": "url",
"type": "string"
},
{
"name": "duration",
"type": "real"
}
],
"rows": [
[
"2019-04-08T13:09:52.871Z",
"244",
"Internal",
"https://google.com",
1245,
]
]
}
]
}
结果:
timestamp id name url duration
2019-04-08 244 Internal https://google.com 1245
这里,在 sql 服务器中,它应该从列中获取列名,从行中获取每列的值
假设您将 json 存储到名为 json.txt
的文件中import json
with open('json.txt') as f:
data = json.load(f)
tableName = data['tables'][0]["name"]
sqlCreateTable = 'CREATE TABLE ' + tableName + ' (\n'
sqlInsertInto = 'INSERT INTO ' + tableName + ' ('
for i in range(0,len(data['tables'][0]['columns'])):
columnName = data['tables'][0]['columns'][i]['name']
type = data['tables'][0]['columns'][i]['type']
sqlCreateTable += columnName + " " + type + ',\n'
sqlInsertInto += columnName + ', '
sqlCreateTable = sqlCreateTable[:-2] + '\n);'
sqlInsertInto = sqlInsertInto[:-2] + ')\nVALUES ('
for value in data['tables'][0]['rows'][0]:
sqlInsertInto += str(value) + ', '
sqlInsertInto = sqlInsertInto[:-2] + ');'
print(sqlCreateTable)
print(sqlInsertInto)
创建 table 的输出:
CREATE TABLE PrimaryResult (
timestamp datetime,
id string,
name string,
url string,
duration real
);
插入 table 的输出:
INSERT INTO PrimaryResult (timestamp, id, name, url, duration)
VALUES (2019-04-08T13:09:52.871Z, 244, Internal, https://google.com, 1245);