I know this is a simple problem but I am new to coding and could use a hand. I need to import a json file into a variable called 'test' for use in the lower half of the code.
import json
CRITICAL = 0
MEDIUM = 1
HIGH = 0
f = open('ECR_scan.json', )
test = json.load(f)
resource = test["resources"][0]
finding_severity_counts = test["detail"]["finding-severity-counts"]
if "CRITICAL" in finding_severity_counts:
critical_count = finding_severity_counts["CRITICAL"]
if critical_count > CRITICAL:
print("Resource {} has {} critical findings".format(resource, critical_count))
if "MEDIUM" in finding_severity_counts:
medium_count = finding_severity_counts["MEDIUM"]
if medium_count > MEDIUM:
print("Resource {} has {} medium findings".format(resource, medium_count))
if "HIGH" in finding_severity_counts:
high_count = finding_severity_counts["HIGH"]
if high_count > HIGH:
print("Resource {} has {} high findings".format(resource, high_count))
I cant get this code to work without spitting out an error. I was hoping someone could help.
Here is the json file I am trying to open.
{
"version": "0",
"id": "85fc3613-e913-7fc4-a80c-a3753e4aa9ae",
"detail-type": "ECR Image Scan",
"source": "aws.ecr",
"account":
"123456789012",
"time": "2019-10-29T02:36:48Z",
"region": "us-east-1",
"resources": [
"arn:aws:ecr:us-east-1:123456789012:repository/my-repo"
],
"detail": {
"scan-status": "COMPLETE",
"repository-name": "my-repo",
"finding-severity-counts": {
"CRITICAL": 10,
"MEDIUM": 9
}
}
}
and here is the traceback error I am getting
Traceback (most recent call last):
File "/home/ec2-user/environment/Final ECR test.py", line 8, in <module>
test = json.load(f)
File "/usr/lib64/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 17 column 20 (char 487)
json.load(f)if you're reading from a fileloadstakes a string. But what is the error that you're seeing?json.loadas you did to load it. it worked. the problem must be in the file itself