【问题标题】:How can I format Amazon Ions to valid JSON in Python?如何在 Python 中将 Amazon Ions 格式化为有效的 JSON?
【发布时间】:2020-01-02 21:29:55
【问题描述】:

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

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

【问题讨论】:

    标签: python json amazon-ion


    【解决方案1】:

    您可以使用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": []
     }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-06
      • 2015-06-16
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2016-07-27
      相关资源
      最近更新 更多