Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法将下面嵌套的json数据插入sql server_Sql_Json_Sql Server_Ssis_Ssis 2012 - Fatal编程技术网

有没有办法将下面嵌套的json数据插入sql server

有没有办法将下面嵌套的json数据插入sql server,sql,json,sql-server,ssis,ssis-2012,Sql,Json,Sql Server,Ssis,Ssis 2012,我正在处理一个嵌套的JSON数据,需要将其加载到SQL SERVER 2012中。嵌套的JSON包含两个根,即一列和另一行。我需要将行中的值放入列中。请参见以下结构: { "tables": [ { "name": "PrimaryResult", "columns": [ { "name": "timestamp",

我正在处理一个嵌套的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 server中,假定您将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)
创建表的输出:

CREATE TABLE PrimaryResult (
timestamp datetime,
id string,
name string,
url string,
duration real
);
插入到表中的输出:

INSERT INTO PrimaryResult (timestamp, id, name, url, duration)
VALUES (2019-04-08T13:09:52.871Z, 244, Internal, https://google.com, 1245);

这是一个复杂的问题,但Phil Factor不久前分享了一些解决方案。是的,路易斯,这是一个有点复杂的场景,实际上这些值不是在键值中定义的,而是像数组一样处理。感谢克里斯蒂安·艾科布的解决方案。实际上执行它花了一点时间,但最终使用上面的C#代码得到了结果。非常感谢你的帮助。