# Convert evaled string to JSON

I am getting a (suppossed to be) JSON string like this : (sJSON)

{
'ARN': 'arn:aws:xxx:xxx:secret:xxx',
'Name': 'xxx/local',
'VersionId': 'xxx-xxx-xxx-xxx-xxx',
'SecretString': 'E:\\path\\to\\folder\\xxx.json',
'VersionStages': ['AWSCURRENT'],
'CreatedDate': datetime.datetime(2022, 9, 28, 8, 25, 35, 329448, tzinfo = tzlocal()),
'RequestId': 'xxx',
'HTTPStatusCode': 200,
'content-type': 'application/x-amz-json-1.1',
'content-length': '306',
'x-amzn-requestid': 'xxx',
'connection': 'close',
'access-control-allow-origin': '*',
'date': 'Wed, 28 Sep 2022 02:56:14 GMT',
'server': 'hypercorn-h11'
},
'RetryAttempts': 0
}
}


I need to parse this as json.loads(sJSON)

How can I first eval this (for datetime.datetime(2022, 9, 28, 8, 25, 35, 329448, tzinfo = tzlocal())) and then convert numbers (last value is a number not any quotes) to string and then convert single quote (') to double quote (") ?

The data isn’t in a JSON format. It is Python. Attempting to force it into json.load() isn’t a wise strategy. You could do it with re.sub but that would be fragile. Instead, load the data directly with an import or eval() as long as the data is from a trusted source.

The data is from an amazon service - awswallet - so its trusted. But I don’t get why its being sent as code and not as a proper string.

Are you sure that some step in your workflow hasn’t already converted the JSON to Python?

The documentation for AWS-JSON-1.1 indicates that it is JSON. What you’ve posted looks like what you get after it has been decoded either with json.load() or with AWS Smithy tooling.

In particular, the datetime object with tzinfo = tzlocal() is a dead giveaway that this is actual Python and not an AWS language independent response format.