繁体   English   中英

遍历从 S3 下载的 json 文件中的对象数组

[英]Looping through array of objects in json file downloaded from S3

我正在使用 boto3 get_object 从 S3 获取一个 json 文件。 我需要从文件中获取内容并遍历对象数组并一次获取一个 object。 当我循环时,我每次迭代得到一个字符。

导入 json 导入 boto3

s3 = boto3.client('s3') session = boto3.Session()

def lambda_handler(event, context):
    bucket = event["bucket"]
    key = event["key"]

    data = s3.get_object(Bucket=bucket, Key=key)
    contents = data['Body'].read()

    test = contents.decode("utf-8")

    # convert contents to native python string representing json object
    s3_string = json.dumps(contents.decode("utf-8"))

    # return dict
    s3_dict = json.loads(s3_string)

    # this seems to output valid json
    # print(str(s3_dict))

    for item in s3_dict:
        print(item)

文件中的json格式如下

[{
        "location": "123 Road Dr",
        "city_state": "MyCity ST",
        "phone": "555-555-5555",
        "distance": "1"
    },
    {
        "location": "456 Avenue Crt",
        "city_state": "MyTown AL",
        "phone": "555-867-5309",
        "distance": "0"
    }
]

这就是我得到的(每次迭代一个字符)...

  1. [
  2. {
  3. “……

这就是我需要的(json 格式)...

第一个循环

{
        "location": "123 Road Dr",
        "city_state": "MyCity ST",
        "phone": "555-555-5555",
        "distance": "1"
    }

第二循环

 {
        "location": "456 Avenue Crt",
        "city_state": "MyTown AL",
        "phone": "555-867-5309",
        "distance": "0"
    }

有人可以告诉我哪里出错了吗?

提前致谢。

这是可行的解决方案。

def lambda_handler(event, context):
    bucket = event["bucket"]
    key = event["key"]

    data = s3.get_object(Bucket=bucket, Key=key)
    contents = data['Body'].read()

    # convert contents to native python string representing json object
    s3_string = contents.decode("utf-8")

    # check the "type" of s3_string - in this case it is <class 'str'>
    print("s3_string is " + str(type(s3_string)))

    # return python list
    s3_list = json.loads(s3_string)

    # check the "type" of s3_list - in this case it is <class 'list'>
    print("s3_list is " + str(type(s3_list)))

    # this returns valid json for every object in the array in original json file.
    for item in s3_list:
        print(json.dumps(item)) 

我假设我在 json.loads 的默认行为中得到了 python 字典。 我实际上得到了一份清单。 这解释了为什么... Json 文件作为列表而不是字典加载。 使用 python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM