Home > database >  JSON string from a 'kind' of JSON string that needs to be evaled
JSON string from a 'kind' of JSON string that needs to be evaled

Time:09-29

I have a string (str_json) that I'm getting from elsewhere :

{
    'ARN': 'arn:aws:secretsmanager:xxx:xxx:secret:xx/xxx-xxx',
    'Name': 'xxx/local',
    'VersionId': 'xxx-xxx-xxx-xxx-xxx',
    'SecretString': 'E:\\path\\to\\folder\\project\\xxx.json',
    'VersionStages': ['AWSCURRENT'],
    'CreatedDate': datetime.datetime(2022, 9, 28, 8, 25, 35, 329448, tzinfo = tzlocal()),
    'ResponseMetadata': {
        'RequestId': 'xxx',
        'HTTPStatusCode': 200,
        'HTTPHeaders': {
            'content-type': 'application/x-amz-json-1.1',
            'content-length': '306',
            'x-amzn-requestid': 'xxx',
            'connection': 'close',
            'access-control-allow-origin': '*',
            'access-control-allow-methods': 'HEAD,GET,PUT,POST,DELETE,OPTIONS,PATCH',
            'access-control-allow-headers': 'authorization,cache-control,content-length,content-md5,content-type,etag,location,x-amz-acl,x-amz-content-sha256,x-amz-date,x-amz-request-id,x-amz-security-token,x-amz-tagging,x-amz-target,x-amz-user-agent,x-amz-version-id,x-amzn-requestid,x-localstack-target,amz-sdk-invocation-id,amz-sdk-request',
            'access-control-expose-headers': 'etag,x-amz-version-id',
            'date': 'Wed, 28 Sep 2022 02:56:14 GMT',
            'server': 'xxx'
        },
        'RetryAttempts': 0
    }
}

This is a string str_json which is supposed to be JSON but it actually contains some python code and a number value and single quotes.

json.loads will fail.

What I need is the JSON string of str_json.

json_data = json.loads(str_json)

What I've attempted so far :

e = eval(str_json)
e['ResponseMetadata']['RetryAttempts'] = "0"
str_json = '"'.join(str(e).split("'"))
json_data = json.loads(str(str_json))
print(json_data)

CodePudding user response:

You can handle that datetime thingy like this, which converts the datetime into proper json:

def datahandler(obj):
  if isinstance(obj, datetime.datetime):
    return obj.isoformat()
  raise TypeError("Unknown type")

def main(argv):
  client = boto3.client("ec2")
  regions = client.describe_regions()                                    # type(regions)  == dict
  jregions = json.dumps(regions, sort_keys=True, default=datahandler)    # type(jregions) == str
  • Related