Here is my use case, I have a AWS Lambda Python function where I get a sample data like below:
{
"time_script_executed": "2022-09-20 11:03:31",
"nv_official_build": "unknown",
"tool_cmd": "c:\abc\1.2.3.4\tool.exe ts /v /tr http://timestamp.entrust.net/TSS/as /td SHA256 \"sd.dll" 2>&1",
}
In my Lambda I do this
def lambda_handler(event, context):
event_body=event["body"]
input_dict=json.loads("{}".format(event_body))
now = datetime.datetime.now()
input_dict["time_2"]=now.strftime('%Y-%m-%dT%H:%M:%S')
input_json=json.dumps(input_dict)
try:
# I will send the input_json to subsequent steps
...
except ClientError as e:
print(e)
exit(1)
response = {
"statusCode": 200
}
return response
In a nutshell, here is what I am trying to do:
- Convert the incoming data string (that might contain invalid JSON characters in 1 specific attribute) to JSON
- Add new attributes to the JSON
- Send JSON downstream
The issue here is since tool_cmd will have some unescaped JSON String that will lead to json.loads errors. If I use json.dumps before I use json.loads (to escape the JSON), it is escaping all double quotes in the JSON but I just want to escape the tool_cmd attribute. Any straightforward way to do this in Python without having to use Regular Expressions ?
