簡體   English   中英

如何在 Python 中將 Amazon Ions 格式化為有效的 JSON?

[英]How can I format Amazon Ions to valid JSON in Python?

在 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?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM