如何在Python中将Amazon Ions格式化为有效的JSON?

问题描述 投票:2回答:1

在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,以便将其返回到来自长生不老药应用程序的函数调用。

所以我尝试了以下代码:

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中Link to the Cookbook是如何将离子下转换为用Java代码编写的JSON的示例,但我无法在python或Amazon QLDB Ledger数据库的python驱动程序中重现该示例。

所以,如何在Python中将Amazon Ions格式化为有效的JSON?

python json amazon-ion
1个回答
0
投票
您可以使用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": [] } ]

© www.soinside.com 2019 - 2024. All rights reserved.