Hagbard
Hagbard

Reputation: 575

How to loop through a JSON file using Python with nested lists and dictionaries

I'm trying to loop through a JSON file using Python and return the name of the object and associated modules for it.

Right now I can basically get the output I want hardcoding the indexes. However, this obviously isn't the right way to do it (the JSON file can vary in length).

Whenever I try to use a loop, I get errors like:

TypeError: string indices must be integers 

My JSON file looks like this:

{
    "name": "gaming_companies",
    "columns": [{
            "name": "publisher",
            "type": "string",
            "cleansing": ["clean_string"]
        },
        {
            "name": "genre",
            "type": "string",
            "cleansing": ["match_genre", "clean_string"]
        },
        {
            "name": "sales",
            "type": "int",
            "cleansing": []
        }
    ]
}

My Python code which is 'working' looks like:

import json as js

def cleansing(games_json):
      print (games_json['columns'][0]['name'] + " - cleansing:")
      [print(i) for i in games_json['columns'][0]['cleansing'] ]
      print (games_json['columns'][1]['name'] + " - cleansing:")
      [print(i) for i in games_json['columns'][1]['cleansing'] ]
      print (games_json['columns'][2]['name'] + " - cleansing:")
      [print(i) for i in games_json['columns'][2]['cleansing'] ]

with open(r'C:\Desktop\gamefolder\jsonfiles\games.json') as input_json:
  games_json = js.load(input_json)
  cleansing(games_json)

The output I'm trying to return is:

publisher
cleansing:
clean_string

genre
cleansing:
match_genre
clean_string

sales 
cleansing:

My attempt to loop through them like this:

      for x in games_json:
          for y in games_json['columns'][x]:
              print (y)

Results in:

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

games_json shows as a Dict.

Columns shows as a list of dictionaries.

Each object's cleansing attribute shows as a list.

I think this is where my problem is, but I'm not able to get over the hurdle.

Upvotes: 1

Views: 365

Answers (3)

YanivGK
YanivGK

Reputation: 913

The problem with your attempt is using an iterator as a string.

The x in for y in games_json['columns'][x]: is an iterator object and not the strings ['name', 'cleansing'].

You can learn more about python iterators here


As for the case - you might want to iterate over the columns as a separate list.

This code should work

for item in f["columns"]:
    print(item["name"])
    print("cleansing:")
    print(item["cleansing"])

Output-

publisher
cleansing:
['clean_string']
genre
cleansing:
['match_genre', 'clean_string']
sales
cleansing:
[]

Upvotes: 1

ComplicatedPhenomenon
ComplicatedPhenomenon

Reputation: 4189

x = """{
    "name": "gaming_companies",
    "columns": [{
            "name": "publisher",
            "type": "string",
            "cleansing": ["clean_string"]
        },
        {
            "name": "genre",
            "type": "string",
            "cleansing": ["match_genre", "clean_string"]
        },
        {
            "name": "sales",
            "type": "int",
            "cleansing": []
        }
    ]
}"""
x = json.loads(x)
for i in x['columns']:
    print(i['name'])
    print("cleansing:")
    for j in i["cleansing"]:
        print(j)
    print('\n')

Output

publisher
cleansing:
clean_string


genre
cleansing:
match_genre
clean_string


sales
cleansing:

with open(r'C:\Desktop\gamefolder\jsonfiles\games.json') as input_json:
    games_json = js.load(input_json)
    for i in games_json['columns']:
        print(i['name'])
        print("cleansing:")
        for j in i["cleansing"]:
            print(j)
        print('\n')

Upvotes: 1

Almazini
Almazini

Reputation: 1873

This can be one of working solutions as you want to iterate array's elements.

import json 

for x in games_json['columns']:
    print(x)
    print(x['name'])

Upvotes: 1

Related Questions