简体   繁体   中英

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

In the example code for the python driver for the Amazon QLDB Ledger database is a function which prints Amazon Ion objects:

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

For my own application I would need to convert this Amazon Ion objects to JSON in order to return it to a function call from an elixir application.

So I tried the following code:

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

But I don't get valid JSON objects. The result of the function above is:

['{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}']

When I try to convert the Amazon Ion object via json.dump like

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

I get the following 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}"']

In both cases I don't get valid JSON objects.

In the Amazon Ion Docs/Cookbook Link to the Cookbook is an example of how to Down-converting Ion to JSON written in Java code but I'm not able to reproduce this in python or with the python driver of the Amazon QLDB Ledger database.

So, how can I format Amazon Ions to valid JSON in Python?

You can use pyion2json as a tool to convert Ion to JSON. It applies the rules listed in the down-converting cookbook to perform the conversions.

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=' '
    )
)

Will give:

[
 {
  "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": []
 }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM