0

I'm trying to write a Python script to read data from a JSON file, do some calculations with it and then write output to a new JSON file. But I can't seem to automate the JSON reading process. I get this error. Could you please help me with this issue? Thank you very much

print([a[0]][b[1]][c[1]])
TypeError: list indices must be integers or slices, not str

test.json

{
      "male": {
            "jack": {
                  "id": "001",
                  "telephone": "+31 2225 345",
                  "address": "10 Street, Aukland",
                  "balance": "1500"
            },
            "john": {
                  "id": "002",
                  "telephone": "+31 6542 365",
                  "address": "Main street, Hanota",
                  "balance": "2500"
            }
      },

      "female": {
            "kay": {
                  "id": "00",
                  "telephone": "+31 6542 365",
                  "address": "Main street, Kiro",
                  "balance": "500"
            }
      }
}

test.py

with open("q.json") as datafile:
    data = json.load(datafile)

    a = ['male', 'female']
    b = ['jack', 'john', 'kay']
    c = ['id', 'telephone', 'address', 'balance']

    print([a[1]][b[1]][c[1]])
4
  • What do you want to print there? Commented Mar 6, 2019 at 6:48
  • @KlausD. male -> John -> telephone Commented Mar 6, 2019 at 6:51
  • print(data[a[0]][b[1]][c[1]]) ? --> Note: keys are case sensitive Commented Mar 6, 2019 at 6:52
  • If you get json data via data = json.load(datafile), data will be dict type. So you can access male -> John -> telephone with data['Male']['John']['telephone']. Commented Mar 6, 2019 at 6:53

2 Answers 2

1

If I understand you correctly, you really want to print data from the JSON, not your intermediary arrays.

So:

print(data['Male'])  # will print the entire Male subsection
print(data['Male']['Jack'])  # will print the entire Jack record
print(data['Male']['Jack']['telephone'])  # will print Jack's telephone

But to relate that with your intermediary arrays too:

print(data[a[0]])  # will print the entire Male subsection
print(data[a[0]][b[0]])  # will print the entire Jack record
print(data[a[0]][b[0]][c[0]])  # will print Jack's telephone

assuming that you declare a correctly:

a = ['Male', 'Female']  # Notice the capitals
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick reply. I want to relate to arrays because that's how I intend to iterate through the array. Thanks so much :)
You can also iterate through the dictionary: for key, value in data.items(). This will give you males first, then females. iterating again through the value would give you persons and iterating again each person's details
0

I dont know, how you access data in your code, because you directly write hard coded values into a, b and c. In addition, you could print out your test via: print(a[1], b[1], c[1]).

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.