首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中将Amazon Ions格式化为有效的JSON?

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

Stack Overflow用户
提问于 2020-01-03 05:29:56
回答 1查看 986关注 0票数 2

在Amazon QLDB Ledger数据库的python驱动程序的示例代码中,有一个打印Amazon Ion对象的函数:

代码语言:javascript
复制
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应用程序的函数调用。

因此,我尝试了以下代码:

代码语言:javascript
复制
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对象。上述函数的结果是:

代码语言:javascript
复制
['{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转换亚马逊离子对象时,比如

代码语言:javascript
复制
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

我得到了以下结果:

代码语言:javascript
复制
['"{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?

EN

回答 1

Stack Overflow用户

发布于 2020-01-15 16:38:46

您可以使用pyion2json作为工具将Ion转换为JSON。它应用down-converting cookbook中列出的规则来执行转换。

代码语言:javascript
复制
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=' '
    )
)

将给予:

代码语言:javascript
复制
[
 {
  "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": []
 }
]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59570255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档