-1

I am writing a python script for extracting information from the json file. I am printing the title of the book with lastname as Marcus. I have the output but it has the error AttributeError: 'str' object has no attribute 'items' error as well

import json
from pprint import pprint
with open('bibliography.json.txt', encoding='utf-8') as data_file:    
data = json.load(data_file)
for entry in data['bibliography']['biblioentry']:
for authors in entry['author']:
for key,val in authors.items():
if(key== 'lastname' and val=='Marcus'):
title=entry['title']
print(title) 

the json file looks like this:

{
  "bibliography": {
    "biblioentry": [
      {
        "-type": "Journal Article",
        "title": "A brief survey of web data extraction tools",
        "author": [
          {
            "firstname": "Alberto",
            "middlename": "HF",
            "lastname": "Laender"
          },
          {
            "firstname": "Berthier",
            "middlename": "A",
            "lastname": "Ribeiro-Neto"
          },
          {
            "firstname": "Altigran",
            "middlename": "S",
            "lastname": "da Silva"
          },
          {
            "firstname": "Juliana",
            "middlename": "S",
            "lastname": "Teixeira"
          }
        ],
        "details": {
          "journalname": "ACM Sigmod Record",
          "volume": "31",
          "number": "2",
          "pages": "84-93"
        },
        "year": "2002",
        "publisher": "ACM"
      },......
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
user8720570
  • 75
  • 1
  • 8
  • 1
    Hi and welcome to StackOverflow. Please refer to [stackoverflow.com/help/how-to-ask](http://stackoverflow.com/help/how-to-ask) on how to ask a proper question and improve yours according the guidelines. A first step could be correctly format yours snippets to make the more readable, this will help other users to help you. – Mindsers Oct 04 '17 at 14:28

1 Answers1

2

I think it is because it interprets the json file as a string. I think you might want to see this if it helps you: Extract data from JSON API using Python

Prometheus
  • 618
  • 1
  • 7
  • 23