在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转换亚马逊离子对象时,比如
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中,Link to the Cookbook是一个如何将离子下转换为JSON的示例,它是用Java代码编写的,但我无法用python或Amazon QLDB Ledger数据库的python驱动程序重现这一过程。
那么,如何在Python中将Amazon Ions格式化为有效的JSON?
发布于 2020-01-15 16:38:46
您可以使用pyion2json作为工具将Ion转换为JSON。它应用down-converting cookbook中列出的规则来执行转换。
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": []
}
]https://stackoverflow.com/questions/59570255
复制相似问题