【问题标题】:Parse JSON document to records for PostgreSQL with Python使用 Python 将 JSON 文档解析为 PostgreSQL 的记录
【发布时间】:2022-01-06 08:11:48
【问题描述】:

我正在尝试将 JSON 文档解析为记录,以使用 Python 将它们存储在 PostgreSQL 中。我是新来的。试图把两个字符串放在一起。一个标头字符串和一个值字符串。具有嵌套词汇表的 JSON 文档的结构很困难。

# import Python's JSON lib
import json

# import the new JSON method from psycopg2
from psycopg2.extras import Json

dict = {"results": [
        {
            "communication_type": "ChatSite",
            "conversation": [
                {
                    "created_at": "2021-11-26 23:30:20",
                    "id": "b29530e3-69ff-4798-abb1-abc17d4d44b5",
                    "int_referer": "link1",
                    "result": "failure",
                    "visitor_id": "account:206867:site:167330:visitor:ybrr4e43f3hj8aor"
                }
            ],
            "duration": 53,
            "first_answer_time": null
        },
        {
            "communication_type": "ChatSite",
            "conversation": [
                {
                    "created_at": "2021-11-26 23:34:00",
                    "id": "e8f7e9bf-e836-4643-a30c-8bcbeffc397a",
                    "int_referer": "link2",
                    "result": "failure",
                    "visitor_id": "account:206867:site:167330:visitor:iosbe9bfqbfswcdi"
                }
            ],
            "duration": 16,
            "first_answer_time": null
        },
    ]
}

a = list(dict.values())   
b = a[0]

# use JSON loads to create a list of records
record_list = json.loads(b)

# create a nested list of the records' values
values = [list(x.values()) for x in record_list]

# get the column names
columns = [list(x.keys()) for x in record_list][0]
##print(columns)
# value string for the SQL string
values_str = ""

# enumerate over the records' values
for i, record in enumerate(b):

    # declare empty list for values
    val_list = []
   
    # append each value to a new list of values
    for v, val in enumerate(record):
        if type(val) == str:
            val = str(Json(val)).replace('"', '')
        val_list += [ str(val) ]
      
    # put parenthesis around each record string
    values_str += "(" + ', '.join( val_list ) + "),\n"
   
# remove the last comma and end SQL with a semicolon
values_str = values_str[:-2] + ";"
#print(values_str)
# concatenate the SQL string
table_name = "json_data"
sql_string = "INSERT INTO %s (%s)\nVALUES %s" % (
    table_name,
    ', '.join(columns),
    values_str
)
print (sql_string)

请帮我解决一下?

Traceback (most recent call last):
  File "c:\Dev\livetex-master\Two.py", line 45, in <module>
    record_list = json.loads(b)
  File "C:\Users\ANISA4\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list

我正在尝试输出一个标题字符串,例如: “communication_type”、“created_at”、“id”、“int_referer”、“result”、“visitor_id”、“duration”、“first_answer_time”

【问题讨论】:

    标签: python json postgresql parsing


    【解决方案1】:

    dict 不是 json 字符串,你必须拥有原始格式,然后用 json.loads() => json load string 加载它

    {
                "communication_type": "ChatSite",
                "conversation": [
                    {
                        "created_at": "2021-11-26 23:30:20",
                        "id": "b29530e3-69ff-4798-abb1-abc17d4d44b5",
                        "int_referer": "link1",
                        "result": "failure",
                        "visitor_id": "account:206867:site:167330:visitor:ybrr4e43f3hj8aor"
                    }
                ],
                "duration": 53,
                "first_answer_time": null
            }
    

    应该是

    "{
                "communication_type": "ChatSite",
                "conversation": [
                    {
                        "created_at": "2021-11-26 23:30:20",
                        "id": "b29530e3-69ff-4798-abb1-abc17d4d44b5",
                        "int_referer": "link1",
                        "result": "failure",
                        "visitor_id": "account:206867:site:167330:visitor:ybrr4e43f3hj8aor"
                    }
                ],
                "duration": 53,
                "first_answer_time": null
            }"
    

    还有

    a = list(dict.values())  
    

    返回列表中的列表,因为您的结果是列表 [[...数据...]]

    【讨论】:

    • a = list(dict.values()) 这一步我走了)
    • 因为您在b 中有列表并且不能是 json.loads()。尝试在某处玩 json.loads() 。试试json.loads("[1,2]") 然后json.loads([1, 2])
    猜你喜欢
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多