繁体   English   中英

使用Python编写Long JSON文档

[英]Write Long JSON document with Python

我需要将json文档写入文件。 这是一项AWS政策,时间有点长,所以我在格式化方面遇到麻烦。

这是我遇到的问题:

def create_iam_policy(user_name):
    # Set the date
    today = datetime.today()
    today = today.strftime("%m-%d-%Y")
    # Set the output file
    output_dir = "../../../json/iam"
    output_file = output_dir + 'pol-aws-secrets-manager-' + user_name + today +'.json'
    create_work_dir(output_dir)

    policy_doc = "{
    \"Version\": \"2012-10-17\",
    \"Statement\": [
        {
            \"Effect\": \"Allow\",
            \"Action\": [
                \"secretsmanager:ListSecrets\",
                \"secretsmanager:GetRandomPassword\"
            ],
            \"Resource\": \"*\"
        },
        {
            \"Effect\": \"Allow\",
            \"Action\": [
                \"kms:Decrypt\"
            ],
            \"Resource\": \"arn:aws:kms:us-east-1:832839043616:key/24260438-1817-4e0b-897c-7f7958edba98\"
        },
        {
            \"Effect\": \"Allow\",
            \"Action\": [
                \"kms:List*\"
            ],
            \"Resource\": \"*\"
        },
        {
            \"Effect\": \"Allow\",
            \"Action\": [
                \"secretsmanager:GetResourcePolicy\",
                \"secretsmanager:GetSecretValue\",
                \"secretsmanager:DescribeSecret\",
                \"secretsmanager:ListSecretVersionIds\"
            ],
            \"Resource\": \"*\",
            \"Condition\": {
                \"ForAnyValue:StringEquals\": {
                    \"secretsmanager:ResourceTag/Name\": user_name
                }
            }
        }
    ]
}"

这是我正在使用的原始json文档: AWS Policy文档

当我运行上面的代码时,出现此错误:

 File ".\aws_iam_rotate_keys.py", line 261
    policy_doc = "{
                  ^
SyntaxError: EOL while scanning string literal

如何正确格式化此长json文档,以免出现错误?

创建一个字典,将其填充并使用json转储将其保存到文件中。

看到这里更多。

import json

policy_doc = {'x':7}
with open('out.json','w') as f:
    json.dump(policy_doc,f)

如果您真的想使用字符串文字,例如,您从其他地方获取JSON,又不想麻烦将其转换为Python对象,而只是将其转换回JSON,请使用三引号因此您可以添加换行符。

如果其中有任何反斜杠,也可以在开头放置一个r ,以避免必须对所有内容进行两次转义。 在这种情况下没有必要。

policy_doc = """{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:ListSecrets",
                "secretsmanager:GetRandomPassword"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt"
            ],
            "Resource": "arn:aws:kms:us-east-1:832839043616:key/24260438-1817-4e0b-897c-7f7958edba98"
        },
        {
            "Effect": "Allow",
            "Action": [
                "kms:List*"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetResourcePolicy",
                "secretsmanager:GetSecretValue",
                "secretsmanager:DescribeSecret",
                "secretsmanager:ListSecretVersionIds"
            ],
            "Resource": "*",
            "Condition": {
                "ForAnyValue:StringEquals": {
                    "secretsmanager:ResourceTag/Name": user_name
                }
            }
        }
    ]
}"""

另一种选择是将JSON放入自己的文件中,然后让您的Python脚本读取它。 这使您在必要时更容易编辑JSON文件,因为您不必担心两种不同语言的语法。

暂无
暂无

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

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