Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading JSON - error
#1
I'm trying to iterate through all of the questions and answers. What am I doing incorrectly?

This works and prints out entire JSON from the top of the hierarchy
import json


with open('D:/trivia_json/dev-v2.0.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())


for i in range (0, len (data['data'])):
    print(data['data']) 
This fails and show the error.
print(data['data']['title'])
TypeError: list indices must be integers or slices, not str

import json


with open('D:/trivia_json/dev-v2.0.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())


for i in range (0, len (data['data'])):
    print(data['data']['title']) 
-------------BELOW IS FIRST PART OF JSON
{
  "version": "v2.0",
  "data": [
    {
      "title": "Normans",
      "paragraphs": [
        {
          "qas": [
            {
              "question": "In what country is Normandy located?",
              "id": "56ddde6b9a695914005b9628",
              "answers": [
                {
                  "text": "France",
                  "answer_start": 159
                },
                {
                  "text": "France",
                  "answer_start": 159
                },
                {
                  "text": "France",
                  "answer_start": 159
                },
                {
                  "text": "France",
                  "answer_start": 159
                }
              ],
              "is_impossible": false
            },
            {
              "question": "When were the Normans in Normandy?",
              "id": "56ddde6b9a695914005b9629",
              "answers": [
                {
                  "text": "10th and 11th centuries",
                  "answer_start": 94
                },
                {
                  "text": "in the 10th and 11th centuries",
                  "answer_start": 87
                },
                {
                  "text": "10th and 11th centuries",
                  "answer_start": 94
                },
                {
                  "text": "10th and 11th centuries",
                  "answer_start": 94
                }
              ],
              "is_impossible": false
            },
Reply
#2
Take a look at this part of json:
Quote:
{
  "version": "v2.0",
  "data": [
    {

data['data'] contains a list of which first item is dictionary, so you could try:
data['data'][0]['title'] instead

Btw you could do it this way:

import json
 
with open('data.json', encoding='utf-8') as f:
    data = json.load(f)

for topic in data['data']:
    print(topic['title'])
    
    for paragraph in topic['paragraphs']:
        #print(paragraph['context'])

        for qa in paragraph['qas']:
            print(qa['question'])
            
            for i, answer in enumerate(qa['answers']):
                print(i, answer['text'])
Reply
#3
thanks

this does show all the titles.
data['data'][i]['title']
i+=1


So to show the first question of each title it would be ['paragraphs']['qas'][0]['question'] right?

(May-22-2019, 06:18 PM)michalmonday Wrote: Take a look at this part of json:
Quote:
{
  "version": "v2.0",
  "data": [
    {

data['data'] contains a list of which first item is dictionary, so you could try:
data['data'][0]['title'] instead

Btw you could do it this way:

import json
 
with open('data.json', encoding='utf-8') as f:
    data = json.load(f)

for topic in data['data']:
    print(topic['title'])
    
    for paragraph in topic['paragraphs']:
        #print(paragraph['context'])

        for qa in paragraph['qas']:
            print(qa['question'])
            
            for i, answer in enumerate(qa['answers']):
                print(i, answer['text'])

That did it. Thanks a million.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 686 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  xlwings error when reading a workbook Mishal0488 1 1,117 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  TypeRoor reading json GreenLynx 3 860 May-16-2023, 01:47 PM
Last Post: buran
  json decoding error deneme2 10 3,660 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  python requests library .JSON() error mHosseinDS86 6 3,443 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,368 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,086 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  I have an issue with Netmiko Error reading SSH protocol banner omarhegazy 2 3,574 May-16-2022, 06:05 PM
Last Post: omarhegazy
  Initializing, reading and updating a large JSON file medatib531 0 1,775 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Help with reading json file hhchenfx 5 4,496 Jul-07-2021, 01:58 PM
Last Post: hhchenfx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020