python - 如何在 Python 中将 Amazon Ions 格式化为有效的 JSON?
问题描述
在 Amazon QLDB Ledger 数据库的 Python 驱动程序的示例代码中,是一个打印 Amazon Ion 对象的函数:
def print_result(cursor):
"""
Pretty print the result set. Returns the number of documents in the result set.
:type cursor: :py:class:`pyqldb.cursor.stream_cursor.StreamCursor`/
:py:class:`pyqldb.cursor.buffered_cursor.BufferedCursor`
:param cursor: An instance of the StreamCursor or BufferedCursor class.
:rtype: int
:return: Number of documents in the result set.
"""
result_counter = 0
for row in cursor:
# Each row would be in Ion format.
logger.info(dumps(row, binary=False, indent=' ',
omit_version_marker=True))
result_counter += 1
return result_counter
对于我自己的应用程序,我需要将此 Amazon Ion 对象转换为 JSON,以便将其返回到来自 elixir 应用程序的函数调用。
所以我尝试了以下代码:
def get_result(cursor):
"""
Pretty print the result set. Returns the number of documents in the result set.
:type cursor: :py:class:`pyqldb.cursor.stream_cursor.StreamCursor`/
:py:class:`pyqldb.cursor.buffered_cursor.BufferedCursor`
:param cursor: An instance of the StreamCursor or BufferedCursor class.
:rtype: int
:return: Number of documents in the result set.
"""
result = []
for row in cursor:
# Each row would be in Ion format.
result.append(dumps(row, binary=False,
omit_version_marker=True))
return result
但我没有得到有效的 JSON 对象。上面函数的结果是:
['{version:\\"BGBl. II Nr. 163/2007\\",valid:true,url:\\"https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279\\",subject:\\"Einweisungsbest\\\\xe4tigung\\",state:\\"in use\\",retrieved_on:null,name:\\"Medizinproduktebetreiberverordnung (MPBV)\\",id:null,country:\\"AT\\",confirmation_template:[]}"', '"{subject:\\"Einweisungsbest\\\\xe4tigung\\",name:\\"Medizinproduktebetreiberverordnung (MPBV)\\",country:\\"AT\\",url:\\"https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279\\",retrieved_on:2019-12-21T00:00:00.000000-00:00,version:\\"BGBl. II Nr. 163/2007\\",state:\\"in use\\",valid:true}']
当我尝试通过 json.dump 转换 Amazon Ion 对象时
def get_result(cursor):
"""
Pretty print the result set. Returns the number of documents in the result set.
:type cursor: :py:class:`pyqldb.cursor.stream_cursor.StreamCursor`/
:py:class:`pyqldb.cursor.buffered_cursor.BufferedCursor`
:param cursor: An instance of the StreamCursor or BufferedCursor class.
:rtype: int
:return: Number of documents in the result set.
"""
result = []
for row in cursor:
# Each row would be in Ion format.
result.append(json.dumps(dumps(row, binary=False,
omit_version_marker=True)))
return result
我得到以下结果:
['"{version:\\"BGBl. II Nr. 163/2007\\",valid:true,url:\\"https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279\\",subject:\\"Einweisungsbest\\\\xe4tigung\\",state:\\"in use\\",retrieved_on:null,name:\\"Medizinproduktebetreiberverordnung (MPBV)\\",id:null,country:\\"AT\\",confirmation_template:[]}"', '"{subject:\\"Einweisungsbest\\\\xe4tigung\\",name:\\"Medizinproduktebetreiberverordnung (MPBV)\\",country:\\"AT\\",url:\\"https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279\\",retrieved_on:2019-12-21T00:00:00.000000-00:00,version:\\"BGBl. II Nr. 163/2007\\",state:\\"in use\\",valid:true}"']
在这两种情况下,我都没有得到有效的 JSON 对象。
在 Amazon Ion Docs/Cookbook中,Cookbook 的链接是一个示例,说明如何将 Ion 下转换为用 Java 代码编写的 JSON,但我无法在 python 中或使用 Amazon QLDB Ledger 数据库的 python 驱动程序来重现这一点。
那么,如何在 Python 中将 Amazon Ions 格式化为有效的 JSON?
解决方案
您可以使用pyion2json作为将 Ion 转换为 JSON 的工具。它应用下转换说明书中列出的规则来执行转换。
import json
import amazon.ion.simpleion as ion
from pyion2json import ion_cursor_to_json
ion_str = '''[
{
version:"BGBl. II Nr. 163/2007",
valid:true,
url:"https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279",
subject:"Einweisungsbest\xe4tigung",
state:"in use",
retrieved_on:null,
name:"Medizinproduktebetreiberverordnung (MPBV)",
id:null,
country:"AT",
confirmation_template:[]
},
{
subject:"Einweisungsbest\xe4tigung",
name:"Medizinproduktebetreiberverordnung (MPBV)",
country:"AT",
url:"https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279",
retrieved_on:2019-12-21T00:00:00.000000-00:00,
version:"BGBl. II Nr. 163/2007",
state:"in use",
valid:true
}
]'''
print(
json.dumps(
ion_cursor_to_json(
ion.loads(ion_str)
),
indent=' '
)
)
会给:
[
{
"version": "BGBl. II Nr. 163/2007",
"valid": true,
"url": "https://www.ris.bka.gv.at/GeltendeFassung.wxe?Abfrage=Bundesnormen&Gesetzesnummer=20005279",
"subject": "Einweisungsbest\u00e4tigung",
"state": "in use",
"retrieved_on": null,
"name": "Medizinproduktebetreiberverordnung (MPBV)",
"id": null,
"country": "AT",
"confirmation_template": []
}
]
推荐阅读
- c++ - 将 *argv[] 中的偶数/奇数元素添加到数组
- python - Numpy 在数组的每个元素上应用条件
- mysql - 子查询左连接引用父ID
- objective-c - Xcode 目标 C:如何从 UIWebView 中清理代码 (ITMS-90809)
- html - CSS当我将鼠标悬停在文本上时,悬停效果消失了
- python - 无法获取设置 Cookie 值 python 请求标头
- amazon-web-services - 是否可以将功能/特定文件从一个 Amazon Connect 实例导入/迁移到另一个实例?
- android - 即使我在环境变量中输入了android sdk的路径,谁能解释一下
- javascript - 简单的任务列表排序 - 如何将其保存到 Firebase Firestore?
- php - 在 mcrypt 上使用谜