1

I have the following json structure in a file:

[
{ "Date": "timevalue", "Org": "b4256282", "Referer": "somevalue" },
{ "Date": "timevalue", "Org": "b4257482", "Referer": "somevalue" },
{ "Date": "timevalue", "Org": "b4253345", "Referer": "somevalue" },
....
]

I want to extract all the Org's.

My code is:

import json

jdata = json.loads(str_of_above_json)
for orgs in jdata['Org']:
     print(orgs)

However this code does not work ... I get the following error messag

TypeError: list indices must be integers, not str

Can anyone let me know what I am doing wrong?

2
  • 1
    for orgs in jdata: print(orgs['Org']) Commented Jul 26, 2017 at 18:34
  • Remember, your data once loaded in as a Python structure is going to be a list of dictionaries. Keyword here is a list. So, Your for loop is just going to iterate over a list, and then each orgs will be the dictionary, which then you can access the 'Org' key. Commented Jul 26, 2017 at 18:38

4 Answers 4

3

You need to iterate over each dictionary in the list and then index dict in turn with dict indexing. For each dictionary in the list,

  1. get the ith dictionary
  2. get the value associated with Org in the ith dictionary
  3. print said value from (2)

In code, this is

for dict_ in jdata:
    org = dict_['Org']
    print(org)

However, we have the power of list comprehension at our disposal, so the code above can more succinctly be represented by,

jdata = json.loads(str_of_above_json)
orgs = [x['Org'] for x in jdata]      
print(orgs)       

Why isn't your current code working?
You do jdata['Org'], but [...] is a dict indexing operation, and this will throw errors because jdata is a list.

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

Comments

2

Remember, your data once loaded in as a Python structure is going to be a list of dictionaries. To keep it simple, it's just:

[{...}, {...}, {...}]

Keyword here is a list. So, your for loop will/should iterate over a list, giving you each dictionary at every iteration.

At this point you can then access the 'Org' key.

So when you do:

for orgs in jdata:
    print(orgs) # each dictionary

At that point, you can now access the Org key in the dictionary:

for orgs in jdata:
    print(orgs) # each dictionary
    print(orgs['Org']) # The value of 'Org' in each dictionary

Comments

0
import json

jdata = json.loads(str_of_above_json)
for orgs in jdata:
    print(orgs["Org"])

You need to iterate over the list [] then print the "org" part of each data.

Comments

0

All the other answers are correct and will solve your current problem.

But if you manipulate a lot with structures like this, you might find the package plucky helpful (full disclosure: I'm the author of that package).

For example (assuming your jdata is loaded):

>>> import plucky
>>> plucky.plucks(data, '.Org')
['b4256282', 'b4257482', 'b4253345']

or:

>>> plucky.pluckable(data).Org
['b4256282', 'b4257482', 'b4253345']

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.