简体   繁体   English

如何将 json“dict”更改为 aws boto3 tagset list of dicts 的可消耗格式

[英]how can I change json "dict" into a consumable format for aws boto3 tagset list of dicts

I have a JSON file that contains a tagset (key, values) in the following format:我有一个 JSON 文件,其中包含以下格式的标记集(键、值):

{"Key1":"ValueA", "Key2":"ValueB"}

The boto3 S3 put_bucket_tagging operation needs to get the tagset in the following format: boto3 S3 put_bucket_tagging操作需要获取如下格式的tagset:

'TagSet': [
            {
                'Key': 'Key1',
                'Value': 'ValueA',
            },
            {
                'Key': 'Key2',
                'Value': 'ValueB',
            }
        ]

It looks like a list of dicts, but I don't know how to get there.它看起来像一个字典列表,但我不知道如何到达那里。 I have tried:我努力了:

    def reformat_json_tagset():
    
        with open('tagset.json') as json_file:
                data = json.load(json_file)
    
    
        info = {}
        content = []
    
        for key in data:
            info = {
                "Key": data[key],
                "Value": data[key],
            }
            content.append(info)
    
        mynewtagset = print("'Tagset': "+ str(content))
        return(mynewtagset)
    
    reformat_json_tagset()

which results in:这导致:

'Tagset': [{'Key': 'Key1', 'Value': 'Key1'}, {'Key': 'Key2', 'Value': 'Key2'}

'Value': 'Key1' and 'Value': 'Key2' are obviously wrong, as they need to be the values. 'Value': 'Key1''Value': 'Key2'显然是错误的,因为它们必须是值。

I understand that the "value": data[key] part in my code is not correct but I don't know how to get the "values" from the json in the values from the tagset.我知道我的代码中的"value": data[key]部分不正确,但我不知道如何从标签集中的值中的 json 获取“值”。 Also I don't know if my approach with the for loop is at all correct, it feels a bit weird to me.另外我不知道我使用 for 循环的方法是否正确,这对我来说有点奇怪。 I'm not stuck to the for loop method, any other suggestion to get from the json to the tagset is more than welcome.我不拘泥于 for 循环方法,任何其他从 json 到标签集的建议都非常受欢迎。

You can get both the key and the value in your iteration by using .items() :您可以使用.items()迭代中的键和值:

content=[]
for k,v in data.items():    
    content.append({"Key":k, "Value":v})
mynewtagset = "'Tagset': "+ str(content)

You could also do this using list comprehension if you like to play code golf:如果你喜欢打代码高尔夫,你也可以使用列表推导来做到这一点:

mynewtagset="'Tagset': "+str([{"Key":k, "Value":v} for k, v in data.items()])

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

相关问题 如何使用boto3更改AWS账户电子邮件? - How can I change the AWS Account Email with boto3? AWS Glue Boto3 - 在哪里可以找到服务员名单? - AWS Glue Boto3 - Where can I find the list of the waiters? 给定aws-lambda,如何使用boto3更改与其相关的cloudwatch规则? - Given an aws-lambda, how can I change the cloudwatch rule associated with it using boto3? 在AWS中,boto3如何列出所有保留,而不管标签和其他值如何 - In AWS, boto3 how can I list all reservations regardless of the tags and other values 如何通过boto3获取aws卷可用大小 - How can i get the aws volumes available size by boto3 我们如何使用 Boto3 列出 aws 参数存储中的所有参数? boto3 文档中没有 ssm.list_parameters 吗? - How can we list all the parameters in the aws parameter store using Boto3? There is no ssm.list_parameters in boto3 documentation? Python / Boto3 / 如何获得带有分页的标签列表? - Python / Boto3 / How can i get a tag list with pagination? 将 AWS DynamoDB 数据转换为 Python/Boto3/Lamba 中的 json 格式 - AWS DynamoDB data to json format in Python/Boto3/Lamba 如何将字典/嵌套字典的字典转换为列表字典 - How do I convert dict of dicts/nested dicts into dict of list 如何使用 boto3 中的 put_item 方法将 dict 列表添加到 DynamoDB - How do I add a list of dict to DynamoDB using the put_item method in boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM