2

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!

Matthew Pope
  • 7,212
  • 1
  • 28
  • 49
Mohan Kumar
  • 63
  • 1
  • 8
  • My understanding: The referenced docs state a sample in java. The principle is not - as you tried- to interpret the ion encoded data as json, but instead to load via e.g. the amazon ion python module the ion data into a python object and then dump the data as json. The docs focus more on the possible precision losses, when storing the data in json as compared to ion. So, ion -> python -> json. – Dilettant Jan 02 '20 at 07:00

2 Answers2

1

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))
A Kosarev
  • 11
  • 1
0

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)


learnerer
  • 396
  • 1
  • 2
  • 17