简体   繁体   English

如何使用 Python 将 Amazon Ion 文件转换为 JSON 格式?

[英]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.

相关问题 使用 Python 将标准 JSON 文件转换为 json-serde 格式并上传到 Amazon Athena(Presto、Hive)的 AWS S3 存储桶 - Convert standard JSON file to json-serde format using Python & upload to AWS S3 bucket for Amazon Athena (Presto, Hive) 使用python将json格式文件转换为tsv - convert json format file to tsv using python 如何用python把txt文件转成json格式? - How to convert txt file to json format with python? 如何使用 Python 将文本转换为 Json 格式 - How to Convert the text into Json Format using Python 如何将不是 csv 文件的 .csv 格式转换为 JSON 格式的 Z23EEEB4347BDD726BFCD6B7AEE9 - How to convert the .csv format which is not a csv file in to JSON format in python? 使用 Python 将 Excel 文件转换为符合特定格式的 Json - Convert Excel file to Json respecting a specific format using Python 使用 python pandas 将 JSON 文件转换为正确的格式 - Convert JSON file into proper format using python pandas 使用 Python 将 CSV 文件数据转换为 JSON 格式 - Convert CSV file data into JSON format using Python 有没有办法使用python将一个大的json文件转换成csv格式 - Is there a way to convert a big json file into csv format using python 如何在亚马逊 lambda 上使用 python 将 csv 转换为 json? - How to convert csv to json with python on amazon lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM