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

5.3k Views Asked by At

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!

2

There are 2 best solutions below

1
On

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)


0
On

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