Search code examples
pythonjsonscopus

TypeError: list indices must be integers (json, python)


The original code was taken from this gist: rivaldi8/ImpactServlet.py but because the api key that I am using does not work with the QUERY_URL, I have modified the code like below:

class Scopus(ImpactService):
    QUERY_URL =  "https://api.elsevier.com/content/search/scopus?query=doi({1})&apiKey={0}&httpAccept=application/json"
    API_KEY = "7f59af901d2d86f78a1fd60c1bf9426a"

    def __init__(self, request):
        self.doi = request.getParameter("doi")

    def _getServiceResponse(self):
        return requests.get(self.QUERY_URL.format(self.API_KEY, self.doi))

    def _buildResponseToClient(self, serviceResponse):
        serviceResponseObject = json.loads(serviceResponse.text)
        citationCount = serviceResponseObject["search-results"]["entry"]["0"]["citedby-count"]
        linkBack = self._getLinkBack(serviceResponseObject)
        return self._getJsonDocument(citationCount, linkBack)

    def _getLinkBack(self, scopusData):
        linkList = scopusData["search-results"]["entry"]["0"]["link"]
        linkBackObject = next(link for link in linkList if link["@ref"] == "scopus-citedby")
        return linkBackObject["@href"]

A sample JSON data below which can be retrieved from this URL:

{
  "search-results": {
    "opensearch:totalResults": "1",
    "opensearch:startIndex": "0",
    "opensearch:itemsPerPage": "1",
    "opensearch:Query": {
      "@role": "request",
      "@searchTerms": "doi(10.1038/35016500)",
      "@startPage": "0"
    },
    "link": [
      {
        "@_fa": "true",
        "@ref": "self",
        "@href": "https://api.elsevier.com/content/search/scopus?start=0&count=25&query=doi%2810.1038%2F35016500%29&apiKey=7f59af901d2d86f78a1fd60c1bf9426a&httpAccept=application/json",
        "@type": "application/json"
      },
      {
        "@_fa": "true",
        "@ref": "first",
        "@href": "https://api.elsevier.com/content/search/scopus?start=0&count=25&query=doi%2810.1038%2F35016500%29&apiKey=7f59af901d2d86f78a1fd60c1bf9426a&httpAccept=application/json",
        "@type": "application/json"
      }
    ],
    "entry": [
      {
        "@_fa": "true",
        "link": [
          {
            "@_fa": "true",
            "@ref": "self",
            "@href": "https://api.elsevier.com/content/abstract/scopus_id/0034729765"
          },
          {
            "@_fa": "true",
            "@ref": "author-affiliation",
            "@href": "https://api.elsevier.com/content/abstract/scopus_id/0034729765?field=author,affiliation"
          },
          {
            "@_fa": "true",
            "@ref": "scopus",
            "@href": "https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
          },
          {
            "@_fa": "true",
            "@ref": "scopus-citedby",
            "@href": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
          }
        ],
        "prism:url": "https://api.elsevier.com/content/abstract/scopus_id/0034729765",
        "dc:identifier": "SCOPUS_ID:0034729765",
        "eid": "2-s2.0-0034729765",
        "dc:title": "Effect of aquaculture on world fish supplies",
        "dc:creator": "Naylor R.",
        "prism:publicationName": "Nature",
        "prism:issn": "00280836",
        "prism:volume": "405",
        "prism:issueIdentifier": "6790",
        "prism:pageRange": "1017-1024",
        "prism:coverDate": "2000-06-29",
        "prism:coverDisplayDate": "29 June 2000",
        "prism:doi": "10.1038/35016500",
        "citedby-count": "1386",
        "affiliation": [
          {
            "@_fa": "true",
            "affilname": "Stanford University",
            "affiliation-city": "Palo Alto",
            "affiliation-country": "United States"
          }
        ],
        "pubmed-id": "10890435",
        "prism:aggregationType": "Journal",
        "subtype": "re",
        "subtypeDescription": "Review",
        "source-id": "21206"
      }
    ]
  }
}

Basically, I want to retrieve the "citedby-count" value which is 1386 and the url in link where ["@ref"] == "scopus-citedby" ie "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward". So the expected output should return like this:

{
  "citationCount": 1386,
  "linkBack": "https://www.scopus.com/inward/citedby.uri?partnerID=HzOxMe3b&scp=0034729765&origin=inward"
}

The error that I have with the code above is:

citationCount = serviceResponseObject["search-results"]["entry"]["0"]["citedby-count"]
    TypeError: list indices must be integers

How to resolve the TypeError: list indices must be integers issue?

Thanks in advance.


Solution

  • You have used string index instead of integer index Change

    citationCount = serviceResponseObject["search-results"]["entry"]["0"]["citedby-count"]
    

    to

    citationCount = serviceResponseObject["search-results"]["entry"][0]["citedby-count"]