0

I have a JSON file that contains some data. testJSON.json

{
"hourlyData": [
    {
        "frequency": "49.96",
        "actual": " 2,240.43 ",
        "schedule": " 2,223.85 ",
        "acp": "325"
    },
    {
        "frequency": "50.04",
        "actual": " 1,862.88 ",
        "schedule": " 1,881.09 ",
        "acp": "275"
    },
    {
        "frequency": "50.04",
        "actual": " 1,882.17 ",
        "schedule": " 1,885.94 ",
        "acp": "275"
    }
  ],
"storageData": [
    {
        "config": "ESS1",
        "name": "BRPL",
        "Power Rating": "20",
        "Energy Rating": "20",
        "socLow": "0",
        "socHigh": "0.8",
        "Charge Eff": "0.9273",
        "Discharge Eff": "0.9273",
        "Round Trip Eff": "0.922",
        "Lower Limit": "57",
        "Mid Limit": "76",
        "High Limit": "95",
        "Thrushold": "5",
        "Discharging Price": "6"
    }
],

I want to store these values into python variables. So what I have done is that first I created a dictionary which contain different variables of different types then I created a function which simple opens the JSON file then I try to store those json values into declared variables: test.py

import json
#decalaring variables 
storageData = {
    "name": 'No name specified',
    "powerRating":-1,
    "energyRating":-1,
    "annualMaxCycles":365,
    "socLow":-1,
    "socHigh":-1,
    "chargeEff":-1,
    "dChargeEff":-1,
    "lowerLimit": -1,
    "midLimit": -1,
    "highLimit": -1,
    "thrushold": 5,
    "dischargingPrice": 6
}
marketData = {
    "marketProducts": {
            "dsm":{
            "frequency":[],
            "schedule":[],
            "actual":[],
            "acp": [],
            }
          }
inputMode = 'JSON'
JSONfileName = "testJSON.json"

def inputJSON():
  if (inputMode == 'JSON'):
    fileName = JSONfileName
    # Import data from JSON files
    with open(JSONfileName, 'r') as myfile:
      dataJSON = ((myfile.read().replace('\n', '')))
    inputJSON = json.loads(dataJSON)

    # Assigning the storageData data
    storageData['powerRating'] = inputJSON['storageData']['Power Rating']
    storageData['energyRating'] = inputJSON['storageData']['energyRating']
    storageData['warranty'] = inputJSON['storageData']['powerRating']
    storageData['annualMaxCycles'] = inputJSON['storageData']['maxAnnualCycles']
    storageData['socLow'] = inputJSON['storageData']['socLow']
    storageData['socHigh'] = inputJSON['storageData']['socHigh']
    storageData['chargeEff'] = inputJSON['storageData']['chargeEff']
    storageData['dChargeEff'] = inputJSON['storageData']['dChargeEff']
    storageData['lowerLimit'] = inputJSON['storageData']['lowerLimit']
    storageData['midLimit'] = inputJSON['storageData']['midLimit']
    storageData['highLimit'] = inputJSON['storageData']['highLimit']
    storageData['thrushold'] = inputJSON['storageData']['thrushold']
    storageData['dischargingPrice'] = inputJSON['storageData']['dischargingPrice']

    marketData['marketProducts']['dsm']['frequency'] = inputJSON['hourlyData']['frequency']
    marketData['marketProducts']['dsm']['acp'] = inputJSON['hourlyData']['acp']
    marketData['marketProducts']['dsm']['actual'] = inputJSON['hourlyData']['actual']
    marketData['marketProducts']['dsm']['schedule'] = inputJSON['hourlyData']['schedule']

inputJSON()

error that it gives me

Traceback (most recent call last):
  File "C:/Users/nvats/PycharmProjects/dsm-final/test2.py", line 113, in <module>
    inputJSON()
  File "C:/Users/nvats/PycharmProjects/dsm-final/test2.py", line 80, in inputJSON
    storageData['powerRating'] = inputJSON['storageData']['Power Rating']
TypeError: list indices must be integers or slices, not str
2
  • 5
    storage_data = inputJSON['storageData'][0] and the use it. Commented Feb 10, 2020 at 9:21
  • 1
    Look at your json file, "storageData" is an array not a dictionary. Commented Feb 10, 2020 at 9:24

1 Answer 1

1

Instead of

# Assigning the storageData data
storageData['powerRating'] = inputJSON['storageData']['Power Rating']
storageData['energyRating'] = inputJSON['storageData']['energyRating']
storageData['warranty'] = inputJSON['storageData']['powerRating']
storageData['annualMaxCycles'] = inputJSON['storageData']['maxAnnualCycles']
storageData['socLow'] = inputJSON['storageData']['socLow']
storageData['socHigh'] = inputJSON['storageData']['socHigh']
storageData['chargeEff'] = inputJSON['storageData']['chargeEff']
storageData['dChargeEff'] = inputJSON['storageData']['dChargeEff']
storageData['lowerLimit'] = inputJSON['storageData']['lowerLimit']
storageData['midLimit'] = inputJSON['storageData']['midLimit']
storageData['highLimit'] = inputJSON['storageData']['highLimit']
storageData['thrushold'] = inputJSON['storageData']['thrushold']
storageData['dischargingPrice'] = inputJSON['storageData']['dischargingPrice']

Do

 # Assigning the storageData data
storageData['powerRating'] = inputJSON['storageData[0]']['Power Rating']
storageData['energyRating'] = inputJSON['storageData[0]']['energyRating']
storageData['warranty'] = inputJSON['storageData[0]']['powerRating']
storageData['annualMaxCycles'] = inputJSON['storageData[0]']['maxAnnualCycles']
storageData['socLow'] = inputJSON['storageData[0]']['socLow']
storageData['socHigh'] = inputJSON['storageData[0]']['socHigh']
storageData['chargeEff'] = inputJSON['storageData[0]']['chargeEff']
storageData['dChargeEff'] = inputJSON['storageData[0]']['dChargeEff']
storageData['lowerLimit'] = inputJSON['storageData[0]']['lowerLimit']
storageData['midLimit'] = inputJSON['storageData[0]']['midLimit']
storageData['highLimit'] = inputJSON['storageData[0]']['highLimit']
storageData['thrushold'] = inputJSON['storageData[0]']['thrushold']
storageData['dischargingPrice'] = inputJSON['storageData[0]']['dischargingPrice']

Because it's an array, whose 1st index has the json data.

Ex. for "hourlyData"

for(data of inputJSON['hourlyData']){
    marketData['marketProducts']['dsm']['frequency'] = data['frequency']
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

6 Comments

can you please be more specific? storageData is a dictionary which contains different variables like powerRating, energyRating etc. I want to store json data into these variables.
Edited my answer. Sorry I didn't understand it at first.🙃
this works partially. for the storageData variables, it is working but for the hourlyData values, when I try to store frequency, acp, actual and schedule values into marketData variables then it is also showing error.
you have to make a "for loop" that will take all indexes of the "hourlyData". As its also an array but with multiple indexes
Ex.` for(data of hourlyData){marketData['marketProducts']['dsm']['frequency'] = inputJSON['hourlyData']['frequency']}`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.