0

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)
6
  • I see one error. You should use json.load(f) if you're reading from a file loads takes a string. But what is the error that you're seeing? Commented Jul 17, 2020 at 15:15
  • I just edited the post with the error I am getting using load instead of loads Commented Jul 17, 2020 at 15:20
  • john, this error means that you have a problem in your json file. The file itself looks ok and it loads correctly for me. It may be some invisible character at the end of your json file Commented Jul 17, 2020 at 15:24
  • That's very unusual. I've used this exact json before and it has worked in other projects just fine. I will check the file again Commented Jul 17, 2020 at 15:27
  • I copied the json from your question into a file and run python json.load as you did to load it. it worked. the problem must be in the file itself Commented Jul 17, 2020 at 15:28

2 Answers 2

3

json.loads expects a string not a file. Try using json.load instead

with open("ECR_scan.json") as f:
    test = json.load(f)
Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide the traceback error in the original post so we can see the issue you are having?
0

The control character can be allowed inside a string as follows:

f = open('ECR_scan.json', )
test = json.load(f,strict=False)

You can find this in the docs for python 2, or the docs for python 3.

If strict is false (True is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0–31 range, including '\t' (tab), '\n', '\r' and '\0'.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.