简体   繁体   中英

How to convert Amazon Ion file to JSON format using Python?

I want to convert Amazon Ion file from S3 bucket to JSON format.

I am trying following code

import json
import boto3


s3 = boto3.resource('s3')
bucket = s3.Bucket('some/path/')
ion_body = bucket.Object('xxxxxxxxxxxxxx.ion').get()['Body'].read().decode("utf-8")
json.loads(ion_body)

But I am getting following JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2) error. Because in Ion file keys are declared without quotes.

Amazon Ion document says we can down convert Ion to Json. But I didn't get any way. Please help me. Thanks!

You can use pyion2json

import json
import boto3
from pyion2json import ion_to_json

s3 = boto3.resource('s3')
bucket = s3.Bucket('some/path/')
ion_body = bucket.Object('xxxxxxxxxxxxxx.ion').get()['Body'].read().decode("utf-8")
print(ion_to_json(ion_body))

The Ion Cookbook has an example for this ( reference )

In addition to amazon.ion, you'll also need to install jsonconversion.

For your case, you'd do:

import boto3
from amazon.ion.json_encoder import IonToJSONEncoder
from amazon.ion.simpleion import loads
import json


s3 = boto3.client('s3')
ion_body = s3.get_object(Bucket='somebucket', Key='xxxxxxxxxxxxxx.ion')['Body'].read()
json.dumps(ion_body, cls=IonToJSONEncoder)


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