[英]How to convert Amazon Ion file to JSON format using Python?
I want to convert Amazon Ion file from S3 bucket to JSON format.我想将 Amazon Ion 文件从 S3 存储桶转换为 JSON 格式。
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.但我得到以下
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
错误。 Because in Ion file keys are declared without quotes.因为在 Ion 文件中,键是不带引号声明的。
Amazon Ion document says we can down convert Ion to Json.亚马逊 Ion 文档说我们可以将 Ion 下转换为 Json。 But I didn't get any way.
但我没有任何办法。 Please help me.
请帮我。 Thanks!
谢谢!
You can use pyion2json您可以使用 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 ) Ion Cookbook 有一个例子( 参考)
In addition to amazon.ion, you'll also need to install jsonconversion.除了 amazon.ion,您还需要安装 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.