Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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
用Python将json解析为Insert语句_Python_Json_Database - Fatal编程技术网

用Python将json解析为Insert语句

用Python将json解析为Insert语句,python,json,database,Python,Json,Database,我有一个包含几个json记录的文件。我必须解析这个文件并将每个JSON加载到特定的SQL Server表中。但是,该表可能不存在于数据库中,在这种情况下,我还必须在加载之前先创建它。因此,我必须解析json文件,找出字段/列并创建表。然后我必须将JSON反序列化到记录中,并将它们插入创建的表中。但是,需要注意的是,json中的某些字段是可选的,即一个json记录中可能没有字段,但另一个记录中可能有字段。下面是一个包含3条记录的示例文件:- { id : 1001, name : "John

我有一个包含几个json记录的文件。我必须解析这个文件并将每个JSON加载到特定的SQL Server表中。但是,该表可能不存在于数据库中,在这种情况下,我还必须在加载之前先创建它。因此,我必须解析json文件,找出字段/列并创建表。然后我必须将JSON反序列化到记录中,并将它们插入创建的表中。但是,需要注意的是,json中的某些字段是可选的,即一个json记录中可能没有字段,但另一个记录中可能有字段。下面是一个包含3条记录的示例文件:-

{ id : 1001, 
  name : "John", 
  age : 30 
} , 

{ id : 1002,
  name : "Peter",
  age : 25
},

{ id : 1002,
  name : "Kevin",
  age : 35,
  salary : 5000
},
请注意,“薪资”字段仅显示在第三条记录中。结果应该是:-

CREATE TABLE tab ( id int, name varchar(100), age int, salary int );

INSERT INTO tab (id, name, age, salary) values (1001, 'John', 30, NULL)
INSERT INTO tab (id, name, age, salary) values (1002, 'Peter', 25, NULL)
INSERT INTO tab (id, name, age, salary) values (1003, 'Kevin', 35, 5000)

谁能帮我一些指点,因为我是Python新手。谢谢。

在Python中,您可以使用标准库中的
sqlite3
json
执行类似操作

import json
import sqlite3

# The string representing the json.
# You will probably want to read this string in from
# a file rather than hardcoding it.
s = """[
    {
        "id": 1001, 
        "name": "John", 
        "age" : 30 
    }, 
    {
        "id" : 1002,
        "name" : "Peter",
        "age" : 25
    },
    {
        "id" : 1002,
        "name" : "Kevin",
        "age" : 35,
        "salary" : 5000
    }
]"""

# Read the string representing json
# Into a python list of dicts.
data = json.loads(s)


# Open the file containing the SQL database.
with sqlite3.connect("filename.db") as conn:

    # Create the table if it doesn't exist.
    conn.execute(
        """CREATE TABLE IF NOT EXISTS tab(
                id int,
                name varchar(100),
                age int,
                salary int
            );"""
        )

    # Insert each entry from json into the table.
    keys = ["id", "name", "age", "salary"]
    for entry in data:

        # This will make sure that each key will default to None
        # if the key doesn't exist in the json entry.
        values = [entry.get(key, None) for key in keys]

        # Execute the command and replace '?' with the each value
        # in 'values'. DO NOT build a string and replace manually.
        # the sqlite3 library will handle non safe strings by doing this.
        cmd = """INSERT INTO tab VALUES(
                    ?,
                    ?,
                    ?,
                    ?
                );"""
        conn.execute(cmd, values)

    conn.commit()
这将在当前目录中创建名为“filename.db”的文件,并插入条目

要测试表格,请执行以下操作:

# Testing the table.
with sqlite3.connect("filename.db") as conn:
    cmd = """SELECT * FROM tab WHERE SALARY NOT NULL;"""
    cur = conn.execute(cmd)
    res = cur.fetchall()
    for r in res:
        print(r)
你可以试试这个:

import json

TABLE_NAME = "tab"

sqlstatement = ''
with open ('data.json','r') as f:
    jsondata = json.loads(f.read())

for json in jsondata:
    keylist = "("
    valuelist = "("
    firstPair = True
    for key, value in json.items():
        if not firstPair:
            keylist += ", "
            valuelist += ", "
        firstPair = False
        keylist += key
        if type(value) in (str, unicode):
            valuelist += "'" + value + "'"
        else:
            valuelist += str(value)
    keylist += ")"
    valuelist += ")"

    sqlstatement += "INSERT INTO " + TABLE_NAME + " " + keylist + " VALUES " + valuelist + "\n"

print(sqlstatement)
但是,要使其正常工作,您需要更改JSON文件以更正如下语法:

[{  
    "id" : 1001, 
    "name" : "John", 
    "age" : 30 
} , 

{   
    "id" : 1002,
    "name" : "Peter",
    "age" : 25
},

{
    "id" : 1003,
    "name" : "Kevin",
    "age" : 35,
    "salary" : 5000
}]
运行此命令将产生以下输出:

INSERT INTO tab (age, id, name) VALUES (30, 1001, 'John')
INSERT INTO tab (age, id, name) VALUES (25, 1002, 'Peter')
INSERT INTO tab (salary, age, id, name) VALUES (5000, 35, 1003, 'Kevin')

请注意,您不需要指定空值。如果您没有在insert语句中指定列,它应该自动将NULL插入您遗漏的任何列。

也许,SQL数据库不是最佳选择。SQL需要一个固定的模式。请注意,您的示例JSON文件不是有效的JSON。您需要将整个文件封装在一组方括号中,还需要用双引号封装密钥。非常感谢@Jon Warren。工作得很有魅力。。。这正是我需要的。也学到了很多新东西。再次感谢。