首页 > 解决方案 > 使用 Python 将 json 解析为 Insert 语句

问题描述

我有一个包含多个 json 记录的文件。我必须解析这个文件并将每个 json 加载到特定的 SQL-Server 表中。但是,该表可能不存在于数据库中,在这种情况下,我还必须在加载之前先创建它。所以,我必须解析 json 文件并找出字段/列并创建表。然后我将不得不de-serialize将 jsons 插入记录并将它们插入到创建的表中。但是,需要注意的是,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 新手。谢谢。

标签: pythonjsondatabase

解决方案


在 Python 中,您可以使用标准库中的sqlite3and来执行类似的操作。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)

推荐阅读