[英]How to read JSON file containing multiple dictionaries with boto3
I have several JSON files containing multiple dictionaries stored in S3.我有几个 JSON 文件,其中包含存储在 S3 中的多个字典。 I need to access each line and rename some of the keys.
我需要访问每一行并重命名一些键。 I have written the code in my local environment which works flawlessly, but I run into issues using Lambda.
我已经在我的本地环境中编写了完美运行的代码,但是我在使用 Lambda 时遇到了问题。 Usually, I get an
Expecting property name enclosed in double quotes
error.通常,我得到一个
Expecting property name enclosed in double quotes
错误。
Example JSON:示例 JSON:
{
"request": 123,
"key1": [
{
"timestamp_unix": 98321,
"key_2": "Portugal"
}
]
}
{
"request": 456,
"key1": [
{
"timestamp_unix": 35765,
"key_2": "China"
}
]
}
Local code:本地代码:
import json
with open("myfile.json", "r") as f:
my_file = [json.loads(line) for line in f]
for j in my_file:
j[key1][0][key2] = j[key1][0].pop("key_2")
AWS code: AWS 代码:
import boto3
import json
s3 = boto3.resource("s3")
obj = s3.Object("my-bucket", "path_to/myfile.json")
json_string = obj.get()["Body"].read().decode("utf-8") # this is where my json object is read in with single quotes instead of double quotes
my_file = [json.loads(line) for line in json_string] # error error error
I also tried:我也试过:
import boto3
import json
s3_client = boto3.client("s3")
obj = s3_client.get_object(Bucket="my-bucket", Key="path_to/myfile.json")
json_string = obj["Body"].read().decode() # this is where my json object is read in with single quotes instead of double quotes
my_file = [json.loads(line) for line in json_string] # error error error
I removed the encode() option altogether, but this didn't work either.我完全删除了 encode() 选项,但这也不起作用。 I don't want to/can't change the underlying json files and store the dicts in a list.
我不想/不能更改底层 json 文件并将字典存储在列表中。
How can I read in json files with multiple dictionaries with boto3?如何使用 boto3 读取具有多个字典的 json 文件?
The boto3 equivalent of for line in f:
is to use the iter_lines()
method. for line in f:
的 boto3 等效项是使用iter_lines()
方法。
lines = obj.get()["Body"]
my_file = [json.loads(line) for line in lines.iter_lines()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.