Diego Carabajal
Diego Carabajal

Reputation: 59

Get value from a dictionary into a JSON file

I need to get all the bodyHtml and authorId values from the file that appears here: https://drive.google.com/file/d/10EGOAWsw3G5-ETUryYX7__JPOfNwUsL6/view?usp=sharing

I have tried several ways, but I always find the error of: TypeError: list indices must be integers, not str

I've tried several ways, this is my last code:

# -*- coding: utf-8 -*-
import json
import requests
import datetime

data = json.loads(open('file.json').read())

coments = data['headDocument']['content']['id']

for comment in data['headDocument']['content']['content']['bodyHtml']:
    info = comment
print(info)

and get this error:

Traceback (most recent call last):
  File "coments.py", line 16, in <module>
    for comment in data['headDocument']['content']['content']['bodyHtml']:
TypeError: list indices must be integers, not str

Can anyone help with this problem?

Upvotes: 0

Views: 82

Answers (1)

Imre_G
Imre_G

Reputation: 2535

Your headDocument['content'] is a list, so you should loop through it. Like this:

for item in data['headDocument']['content']:
    print(item['content']['bodyHtml'])

Upvotes: 2

Related Questions