0

I have a this json file:

{
 "DailyForecasts": [
  {
   "Temperature": {
    "Minimum": {
     "Value": 2.9
    },
    "Maximum": {
     "Value": 16.1
    }
  },
 "HoursOfSun": 12,
 "AirAndPollen": [
    {
      "Name": "AirQuality",
      "Value": 37,
      "CategoryValue": 1
    },
    {
      "Name": "UVIndex",
      "Value": 5,
      "CategoryValue": 2
    }
   ]
  }
 ]
}

I would like to remove "CategoryValue" key from "AirAndPollen" array. I tried this Python code, but getting:

TypeError: list indices must be integers or slices, not str

My Python code:

import json

with open('accu-test.json') as f:
    data = json.load(f)

for remove in data['DailyForecasts']:
    del remove['AirAndPollen']['CategoryValue']

print(json.dumps(data, indent=2))

What's the recommended approach in this case? Thank you.

4 Answers 4

1

AirAndPollen is a list, you would have to iterate through it too.

for i in data['DailyForecasts']:
    for j in i['AirAndPollen']:
        del j['CategoryValue']

from pprint import pprint
pprint(data)

 # {'DailyForecasts': [{'AirAndPollen': [{'Name': 'AirQuality', 'Value': 37},
 #                                       {'Name': 'UVIndex', 'Value': 5}],
 #                      'HoursOfSun': 12,
 #                      'Temperature': {'Maximum': {'Value': 16.1},
 #                                      'Minimum': {'Value': 2.9}}}]}
Sign up to request clarification or add additional context in comments.

Comments

0

If you add a statement print(remove['AirAndPollen']) you'll see that remove['AirAndPollen'] is not a single object but rather a list. You need an additional loop to operate on each on of those objects in turn.

Comments

0

try this one:

data = {
    "DailyForecasts": [
        {
            "Temperature": {
                "Minimum": {
                    "Value": 2.9
                },
                "Maximum": {
                    "Value": 16.1
                }
            },
            "HoursOfSun": 12,
            "AirAndPollen": [
                {
                    "Name": "AirQuality",
                    "Value": 37,
                    "CategoryValue": 1
                },
                {
                    "Name": "UVIndex",
                    "Value": 5,
                    "CategoryValue": 2
                }
            ]
        }
    ]
}

air_and_pollen = data.get("DailyForecasts")[0]["AirAndPollen"]
for i in air_and_pollen:
    i.pop("CategoryValue")
data.get("DailyForecasts")[0]["AirAndPollen"] = air_and_pollen
print(data)

Comments

0

You could use nested_delete from the nested_lookup module:

NB: I wrapped the json data into a content variable:

from nested_lookup import nested_delete

#specify the key u wish to remove
content = nested_delete(content,'CategoryValue')

print(content)

{'DailyForecasts': [{'Temperature': {'Minimum': {'Value': 2.9},
    'Maximum': {'Value': 16.1}},
   'HoursOfSun': 12,
   'AirAndPollen': [{'Name': 'AirQuality', 'Value': 37},
    {'Name': 'UVIndex', 'Value': 5}]}]}

Comments

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.