简体   繁体   English

如何使用 boto3 读取包含多个字典的 JSON 文件

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

相关问题 从Python中的json文件中读取多个包含整数的字典 - Read multiple dictionaries containing integers from json file in Python 如何使用boto3读取s3存储桶中存在的json文件? - how to read a json file present in s3 bucket using boto3? 如何使用 Boto3 将子级 JSON 文件加载到 DynamoDB? - How to load a sublevel JSON file into DynamoDB using Boto3? 如何将多个字典写入json / txt文件并读回? - How to write multiple dictionaries into json/txt file and read them back? 使用 Python boto3 读取 JSON 文件 - Reading an JSON file using Python boto3 读取包含多个字典的 json 文件 - Read a json file that contains multiple dictionaries Python:如何读取包含多个嵌套词典的.py(配置)文件,进行修改,然后将更新后的内容保存到.py? - Python: how to read .py (config) file containing multiple nested dictionaries, modify, then save updated content to .py? 如何使用boto3只知道文件扩展名来读取s3目录中的文件 - How to read file in s3 directory only knowing file extension using boto3 如何读取一个目录中的多个文件,所有这些文件都是带有Airflow S3 Hook或boto3的csv.gzip? - How to read multiple files in a directory, all of which are csv.gzip with Airflow S3 Hook or boto3? 如何使用 Python 和 Boto3 从 S3 Bucket 读取 Txt 文件 - How to read Txt file from S3 Bucket using Python And Boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM