Home > Enterprise >  Extracting data from a json file into a python file
Extracting data from a json file into a python file

Time:05-17

I want to do a Sobol (GSA) Analysis. For that matter, I want to extract data from my parameters, which are stored in a json file, into my python file (extension: ipynb). I watched a video and a website (https://www.youtube.com/watch?v=9N6a-VLBa2I and https://yourblogcoach.com/how-to-extract-data-from-json-file-in-python/) but the Loop through data step doesnt work. I noticed that in they work with lists, e.g.:

{"web": {
    "languages": [
        {
            "id": "1",
            "name": "PHP",
            "website": "https://www.php.net/"
        },
        {
            "id": "2",
            "name": "Python",
            "website": "https://www.python.org/"
        },
        {
            "id": "3",
            "name": "Java",
            "website": "https://www.java.com/en/"
        }
    ]
}

}

My json file looks a little bit different - I don´t have any lists (with [] brackets). It is just like a dict of objects:

{ "tech_water": { 
      "name": "Water",
      "value": 0.80,
      "description": "",
      "distribution": "normal",
      "arguments": {
        "mu": 0.20,
        "sigma": 0.02
      }
    },
  "tech_extruder": {
    "name": "Red ScaleUp",
    "value": 0.80,
    "description": "",
    "distribution": "normal",
    "arguments": {
      "mu": 0.40,
      "sigma": 0.02
     }
   }
 }

What I did is this:

import json

with open ('path', 'r') as json_file:
    json_load = json.load(json_file)
print(json.dumps(json_load, indent=2))

for parameter in json_load: #object to access the tech_water key
    print(parameter['name'])

And then I get this error:

TypeError
Traceback (most recent call last)
c:\Users\karen...
      1 #Loop through data
      3 for parameter in json_load: #object to access the tech_water key
**----> 4     print(parameter['name'])
TypeError: string indices must be integers

Does someone know why or how I can extract the specific information of each key (e.g. names, distribution and arguments from "tech_water" and "tech_extruder"?

CodePudding user response:

Here is the debugging process I would follow:

Interpret the error

You get the following error: TypeError: string indices must be integers which means parameter is a string.

If you print parameter before the error you would see that parameter contains 'tech_water'

What i guess you expect

I guess you expect to have the full dict

Your misunderstanding

You thought that looping over a dict would give the items, in fact it give you the keys

Possible fixes

Loop over values

for parameter in json_load.values(): 
    print(parameter['name'])

Access value through key

for parameter in json_load:
    print(json_load[parameter]['name'])

CodePudding user response:

If you want to see the keys and values of tech_water, for example, you just do

for keys in json_load['tech_water']:
    print(keys, json_load['tech_water'][keys])

As a dict, your json looks just like the file you format you posted

{'tech_water': {'name': 'Water',
  'value': 0.8,
  'description': '',
  'distribution': 'normal',
  'arguments': {'mu': 0.2, 'sigma': 0.02}},
 'tech_extruder': {'name': 'Red ScaleUp',
  'value': 0.8,
  'description': '',
  'distribution': 'normal',
  'arguments': {'mu': 0.4, 'sigma': 0.02}}}
  • Related