将 API 中的所有页面转换为一个 JSON 响应,并使用 Python 插入到 Snowflake 中的表中
我对 Python 和 Snowflake 都很陌生,我确实阅读了相关主题并尝试了所有这些主题,但它对我不起作用。 该 API 有分页限制,我能够通过使用 Python 中的循环来获取所有页面的所有数据。由于某些 CDC 原因,我想将每个页面的所有 JSON 合并到一个 JSON 对象中,并将其插入到 Snowflake 中的表中。 我使用下面的代码来获取数据并将其推送到 Snowflake:
results=[]
for i in range(1,total_pages+1):
URL="*********/?page={}".format(i)
response=requests.get(URL,headers=headers)
data=response.json()["results"]
for k in data:
results.append(k)
def js(obj):
text = json.dumps(obj, sort_keys=True, indent=4)
return text
final=js(results)
ctx=snowflake.connector.connect(
user='**',
password = '***',
account = '****',
warehouse = '****',
database = '****',
schema = 'LOAD',
role='SYSADMIN')
cs=ctx.cursor()
try:
cs.execute("insert into test_json_load (select PARSE_JSON('%s'),CURRENT_TIMESTAMP(0) )" % json.dumps(final))
finally:
cs.close()
ctx.close()
如果我使用响应,代码可以正常工作,但它将最后一页作为一个 JSON 对象写入表中。当我使用final时,它给我:
ProgrammingError: 001003 (42000): SQL编译错误: 解析错误行 1 位于“34”附近的位置 21,288。 语法错误第 1 行位于位置 21,290 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,319。 第 1 行语法错误位于位置 21,322 处出现意外的“”。 语法错误第 1 行位于位置 21,327 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,352。 语法错误第 1 行位于位置 21,360 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,383。 解析错误行 1 位于“34”附近的位置 21,416。 第 1 行语法错误位于位置 21,418 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,441。 语法错误第 1 行位于位置 21,456 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,486。 解析错误行 1 位于“34”附近的位置 21,519。 第 1 行语法错误位于位置 21,521 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,544。 第 1 行语法错误位于位置 21,551 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,575。 语法错误第 1 行位于位置 21,587 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,608。 解析错误行 1 位于“34”附近的位置 21,618。 语法错误第 1 行位于位置 21,620 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,635。 第 1 行语法错误位于位置 21,643 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,665。 第 1 行语法错误位于位置 21,673 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,694。 解析错误行 1 位于“34”附近的位置 21,707。 第 1 行语法错误位于位置 21,709 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,730。 语法错误第 1 行位于位置 21,738 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,755。 解析错误行 1 位于“34”附近的位置 21,776。 第 1 行语法错误位于位置 21,778 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,807。 第 1 行语法错误位于位置 21,815 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,840。 第 1 行语法错误位于位置 21,848 处出现意外的“\n”。 解析错误第 1 行,位于“34”附近的位置 21,871。 解析错误行 1 位于“34”附近的位置 21,904。 第 1 行语法错误位于位置 21,906 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,929。 解析错误行 1 位于“110”附近的位置 21,937。 第 1 行语法错误位于位置 21,944 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 21,974。 解析错误行 1 位于“34”附近的位置 22,007。 第 1 行语法错误位于位置 22,009 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 22,032。 第 1 行语法错误位于位置 22,039 处出现意外的“\n”。 解析错误行 1 位于“34”附近的位置 22,063。 此输入共有 7995 个错误。
我已经检查了最终文件及其有效的 JSOn。
您能帮我解决这个问题吗?
预先感谢您的帮助。
I am new to both Python and SNowflake, and I did read the related topics and tried all of them, but it doesn't work for me.
The API has a pagination restriction and I was able to get all the data from all pages by using a loop in Python. For some CDC reasons, I want to combine all of the JSONs from each page in ONE JSON object and insert it into a table in Snowflake.
I use the code below for getting the data and pushing it to Snowflake:
results=[]
for i in range(1,total_pages+1):
URL="*********/?page={}".format(i)
response=requests.get(URL,headers=headers)
data=response.json()["results"]
for k in data:
results.append(k)
def js(obj):
text = json.dumps(obj, sort_keys=True, indent=4)
return text
final=js(results)
ctx=snowflake.connector.connect(
user='**',
password = '***',
account = '****',
warehouse = '****',
database = '****',
schema = 'LOAD',
role='SYSADMIN')
cs=ctx.cursor()
try:
cs.execute("insert into test_json_load (select PARSE_JSON('%s'),CURRENT_TIMESTAMP(0) )" % json.dumps(final))
finally:
cs.close()
ctx.close()
If I use the response the code works with no issue, but it writes the last page to the table as one JSON object. When I use the final, it gives me :
ProgrammingError: 001003 (42000): SQL compilation error:
parse error line 1 at position 21,288 near '34'.
syntax error line 1 at position 21,290 unexpected '\n'.
parse error line 1 at position 21,319 near '34'.
syntax error line 1 at position 21,322 unexpected '"'.
syntax error line 1 at position 21,327 unexpected '\n'.
parse error line 1 at position 21,352 near '34'.
syntax error line 1 at position 21,360 unexpected '\n'.
parse error line 1 at position 21,383 near '34'.
parse error line 1 at position 21,416 near '34'.
syntax error line 1 at position 21,418 unexpected '\n'.
parse error line 1 at position 21,441 near '34'.
syntax error line 1 at position 21,456 unexpected '\n'.
parse error line 1 at position 21,486 near '34'.
parse error line 1 at position 21,519 near '34'.
syntax error line 1 at position 21,521 unexpected '\n'.
parse error line 1 at position 21,544 near '34'.
syntax error line 1 at position 21,551 unexpected '\n'.
parse error line 1 at position 21,575 near '34'.
syntax error line 1 at position 21,587 unexpected '\n'.
parse error line 1 at position 21,608 near '34'.
parse error line 1 at position 21,618 near '34'.
syntax error line 1 at position 21,620 unexpected '\n'.
parse error line 1 at position 21,635 near '34'.
syntax error line 1 at position 21,643 unexpected '\n'.
parse error line 1 at position 21,665 near '34'.
syntax error line 1 at position 21,673 unexpected '\n'.
parse error line 1 at position 21,694 near '34'.
parse error line 1 at position 21,707 near '34'.
syntax error line 1 at position 21,709 unexpected '\n'.
parse error line 1 at position 21,730 near '34'.
syntax error line 1 at position 21,738 unexpected '\n'.
parse error line 1 at position 21,755 near '34'.
parse error line 1 at position 21,776 near '34'.
syntax error line 1 at position 21,778 unexpected '\n'.
parse error line 1 at position 21,807 near '34'.
syntax error line 1 at position 21,815 unexpected '\n'.
parse error line 1 at position 21,840 near '34'.
syntax error line 1 at position 21,848 unexpected '\n'.
parse error line 1 at position 21,871 near '34'.
parse error line 1 at position 21,904 near '34'.
syntax error line 1 at position 21,906 unexpected '\n'.
parse error line 1 at position 21,929 near '34'.
parse error line 1 at position 21,937 near '110'.
syntax error line 1 at position 21,944 unexpected '\n'.
parse error line 1 at position 21,974 near '34'.
parse error line 1 at position 22,007 near '34'.
syntax error line 1 at position 22,009 unexpected '\n'.
parse error line 1 at position 22,032 near '34'.
syntax error line 1 at position 22,039 unexpected '\n'.
parse error line 1 at position 22,063 near '34'.
This input had 7995 total errors.
I have checked the final file and its a valid JSOn.
Would you please help me with this problem?
Thank you in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 JSON 文本复制并粘贴到 JSON 格式化程序和验证程序中。这将帮助您分析 JSON 的格式错误的地方。
https://jsonformatter.org/
https://jsonformatter.curiousconcept.com/
https://elmah.io/tools/json-formatter/
https://jsonlint.com/
首先确保它是真正的 JSON,没有单引号,如这个:{'键':'值'}。虽然这看起来像 JSON,但事实并非如此。 JSON 要求所有字符串都使用双引号。
Copy and paste the JSON text into a JSON Formatter and Validator. This will help you analyze where your JSON is badly formed.
https://jsonformatter.org/
https://jsonformatter.curiousconcept.com/
https://elmah.io/tools/json-formatter/
https://jsonlint.com/
First of all make sure that it is real JSON, with no single quotation marks like this: {'key': 'value'}. While this looks like JSON, it is not. JSON requires double-quotes for all strings.