【发布时间】: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