Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to parse JSON from file using Python3

I’m trying to get the value of ["pooled_metrics"]["vmaf"]["harmonic_mean"] from a JSON file I want to parse using python. This is the current state of my code:

for crf in crf_ranges:
    vmaf_output_crf_list_log = job['config']['output_dir'] + '/' + build_name(stream) + f'/vmaf_{crf}.json'
    # read the vmaf_output_crf_list_log file and get value from ["pooled_metrics"]["vmaf"]["harmonic_mean"]
    with open(vmaf_output_crf_list_log, 'r') as json_vmaf_file:
        # load the json_string["pooled_metrics"] into a python dictionary
        vm = json.loads(json_vmaf_file.read())
        vmaf_values.append((crf, vm["pooled_metrics"]["vmaf"]["harmonic_mean"]))

This will give me back the following error:

AttributeError: ‘dict’ object has no attribute ‘loads’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I always get back the same AttributeError not matter if I use "load" or "loads".

I validated the contents of the JSON, which is valid using various online validators, but still, I am not able to load the JSON for further parsing operations.

I expect that I can load a file that contains valid JSON data. The content of the file looks like this:

{
  "frames": [
    {
      "frameNum": 0,
      "metrics": {
        "integer_vif_scale2": 0.997330,
      }
    },
  ],
  "pooled_metrics": {
    "vmaf": {
      "min": 89.617207,
      "harmonic_mean": 99.868023
    }
  },
  "aggregate_metrics": {
  }
}

Can somebody provide me some advice onto this behavior, what does it seem so absolutely impossible to load this JSON file?

>Solution :

loads is a method for the json library as the docs say https://docs.python.org/3/library/json.html#json.loads. In this case you are having a AttributeError this means that probably you have created another variable named "json" and when you call json.loads is calling that variable hence it won’t have a loads method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading