2

I am trying to get the values from count and add them to find the total sum. The code I have written is:

import json
data= '''
{
  "note":" sample data ",
  "comments":
  [
    {
      "School":"UCLA",
      "count":97
    },
    {
      "School":"MIT",
      "count":97
    },
    {
      "School":"Rutgers",
      "count":90
    }
  ]
}'''

number=list()
a=0
b=0
info = json.loads (data)
print json.dumps(info, indent=4)
for i in info:
    number= i["comments"][0]["count"]
for n in number:
    a=float(n)
    b+=a
print b

When I execute this, the output I am getting is:

Traceback (most recent call last):
  File "testty.py", line 28, in <module>
    number= i["comments"][0]["count"]
TypeError: string indices must be integers

Can some one please tell me what I am doing wrong and how to fix it.

Thanks

3
  • Thank you all, its working now. But if I want to get the values using the step: number= i["comments"][0]["count"] , What change should I make for it to work ? Commented May 27, 2016 at 0:48
  • Hi Merlin, I am trying to capture the value of count using the command: number= i["comments"][0]["count"]. But I am getting the error : Traceback (most recent call last): File "testty.py", line 28, in <module> number= i["comments"][0]["count"] TypeError: string indices must be integers Even though my code is working now by using what @Barmar mentioned, I am wondering what changes should I make to the above command so that i can use it in my code. Commented May 27, 2016 at 1:14
  • I am just trying to understand how the command number= i["comments"][0]["count"] , works and why it's not working in this case. Thank you for your time. Commented May 27, 2016 at 1:16

4 Answers 4

3

You should be looping over info["comments"] which is a list. You can also use the sum function to total the values

b = sum(float(i["count"]) for i in info["comments"])
Sign up to request clarification or add additional context in comments.

Comments

2

This is python list comprehension:

tt = sum([float(row['count']) for row in info['comments']])
print tt

or this is "for" loops

tt = [] 
for row in info['comments']:
    tt.append(float(row['count']))

b = sum(tt)
print b

List comprehension is usually faster...

Your code:

    data= '''
[{
  "note":" sample data ",
  "comments":
  [
    {
      "School":"UCLA",
      "count":97
    },
    {
      "School":"MIT",
      "count":97
    },
    {
      "School":"Rutgers",
      "count":90
    }
  ]
}]'''

Use the above as data and your code should work... You are missing opening '[' and closing ']'...

Comments

2

This:

for i in info:

iterates over the dict, which yields keys, which are strings. Access info directly instead.

Comments

1

The top-level JSON object is not an array, so you shouldn't iterate over it. The array is in info["comments"], so do

for i in info["comments"]:
    a = float(i["count"])
    b += a
print b

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.