1

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.

2
  • 1
    Hint: print x at the beginning of each iteration of the outer for loop and consider how that affects the inner loop. Commented Aug 19, 2019 at 12:58
  • 1
    Iterating over a dict like for k in _dict: will return the keys of the dictionary Commented Aug 19, 2019 at 12:59

3 Answers 3

1

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:
[]
Sign up to request clarification or add additional context in comments.

Comments

1

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'])

Comments

1
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')

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.