2

I'm new to Python. Now I have a JSON file. I need first read the file in Python and then do something (make a two way table, etc.) with it. I was able to do this:

import json
with open('DrateLspan.json') as f:
    file = json.load(f)
for i in file:
    print(i['Year'])

However, since I need extract the data from JSON and do something more, I do not want to stop at the "print" step. I tried ye = file['Year'] but it shows "string indices must be integers, not str". Can anyone help me with this? I want use the data in the JSON file.

Below this the first several lines of my JSON file:

[
  {
    "Year": 2015,
    "Race": "All Races",
    "Sex": "Both Sexes",
    "Average Life Expectancy (Years)": "",
    "Age-adjusted Death Rate": 733.1
  },
  {
    "Year": 2014,
    "Race": "All Races",
    "Sex": "Both Sexes",
    "Average Life Expectancy (Years)": 78.9,
    "Age-adjusted Death Rate": 724.6
  },
  {
    "Year": 2013,
    "Race": "All Races",
    "Sex": "Both Sexes",
    "Average Life Expectancy (Years)": 78.8,
    "Age-adjusted Death Rate": 731.9

3 Answers 3

3

It looks like you might have just gotten a bit mixed up on some variable names. You are trying to do:

year = file["Year"]

But the file variable is the list not the individual item. You probably meant:

year = i["Year"]

Something like this should work just fine:

import json

with open(filename) as json_file:
    all_data = json.load(json_file)

for entry in all_data:
    year = entry["Year"]
    # Do something with year

One thing I find helpful to avoid confusing situations like this is to always name my variables something specific. Hope this helps!

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

2 Comments

Thank you. I tried. It worked but it only shows one result, which is 1900. I expect there would be a list of years. Do you have any idea about this?
Sounds like you might be a bit confused about how for loops work. I would check out one of these resources: w3 schools on for loops or geek for geeks on loops
1

try

The json file is array of dicts. so you need to iterate over that list and the access each of the dicts and the year attribute. To collect all the years you can collect that to a list.

If you don't duplicates you can use a set

import json

with open('test.json') as f:
    data = json.load(f)
    year = []
    for item in data:
        year.append(item['Year'])
    print(year)
    #uncomment the below line to remove duplicates
    year = set(year)

1 Comment

Thank you it worked. But the result was only correct when I use >print(year). I got a list of years. However, if I just use >year, I only got one result which is 1900. So confused. Do you have an idea?
0

I would suggest using Panda, which does the job to organize your data conveniently. Look at how easy it can be:

import json
import pandas as pd

with open('json.json') as f:
    data = json.load(f)
    df = pd.DataFrame.from_dict(data)
    print(df)

Which will create (and print) something like:

   Year       Race         Sex Average Life Expectancy (Years)  Age-adjusted Death Rate
0  2015  All Races  Both Sexes                                                    733.1
1  2014  All Races  Both Sexes                            78.9                    724.6
2  2013  All Races  Both Sexes                            78.8                    731.9

Then you can access your data in multiple ways:

  • Individual item: print(df.loc[0,'Year'])
  • By row: print(df.loc[0])
  • By column: df['Year']

It's very flexible. If you need a quick start, this might be a valuable tutorial

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.