Calvin
Calvin

Reputation: 331

Unable to access json data under square bracket - nodejs

I am using nodejs and below are the data returned from Google Book API. I wanted to access the information "Authors" from the data which is under a square bracket, however I am not able to access it. I tried a couple of methods but still fail to access them.

{
    "kind": "books#volumes",
    "totalItems": 42,
    "items": [
        {
            "kind": "books#volume",
            "id": "TpuJxgEACAAJ",
            "etag": "kFhVXIpo2Aw",
            "selfLink": "https://www.googleapis.com/books/v1/volumes/TpuJxgEACAAJ",
            "volumeInfo": {
                "title": "The Guardians",
                "authors": [
                    "John Grisham"
                ],
                "publisher": "Random House Large Print",
                "publishedDate": "2019-10-15",
                "description": "The suspense never rests in John Grisham's pulse-pounding new legal thriller. The latest novel from the New York Times #1 bestselling author moves at breakneck speed, delivering some of his most inventive twists and turns yet. In the small north Florida town of Seabrook, a young lawyer named Keith Russo was shot dead at his desk as he worked late one night. The killer left no clues behind. There were no witnesses, no real suspects, no one with a motive. The police soon settled on Quincy Miller, a young black man who was once a client of Russo's. Quincy was framed, convicted, and sent to prison for life. For twenty-two years he languished in prison with no lawyer, no advocate on the outside. Then he wrote a letter to Guardian Ministries, a small innocence group founded by a lawyer/minister named Cullen Post. Guardian handles only a few innocence cases at a time, and Post is its only investigator. He travels the South fighting wrongful convictions and taking cases no one else will touch. With Quincy Miller, though, he gets far more than he bargained for. Powerful, ruthless people murdered Keith Russo, and they do not want Quincy exonerated. They killed one lawyer twenty-two years ago, and they will kill another one without a second thought.",
                "industryIdentifiers": [
                    {
                        "type": "ISBN_10",
                        "identifier": "0525639381"
                    },
                    {
                        "type": "ISBN_13",
                        "identifier": "9780525639381"
                    }
                ],
                "readingModes": {
                    "text": false,
                    "image": false
                },
                "pageCount": 480,
                "printType": "BOOK",
                "categories": [
                    "Fiction"
                ],
                "maturityRating": "NOT_MATURE",
                "allowAnonLogging": false,
                "contentVersion": "preview-1.0.0",
                "panelizationSummary": {
                    "containsEpubBubbles": false,
                    "containsImageBubbles": false
                },
                "imageLinks": {
                    "smallThumbnail": "http://books.google.com/books/content?id=TpuJxgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
                    "thumbnail": "http://books.google.com/books/content?id=TpuJxgEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
                },
                "language": "en",
                "previewLink": "http://books.google.com.my/books?id=TpuJxgEACAAJ&dq=the+guardian+inauthor:john+grisham&hl=&cd=1&source=gbs_api",
                "infoLink": "http://books.google.com.my/books?id=TpuJxgEACAAJ&dq=the+guardian+inauthor:john+grisham&hl=&source=gbs_api",
                "canonicalVolumeLink": "https://books.google.com/books/about/The_Guardians.html?hl=&id=TpuJxgEACAAJ"
            },
            "saleInfo": {
                "country": "MY",
                "saleability": "NOT_FOR_SALE",
                "isEbook": false
            },
            "accessInfo": {
                "country": "MY",
                "viewability": "NO_PAGES",
                "embeddable": false,
                "publicDomain": false,
                "textToSpeechPermission": "ALLOWED",
                "epub": {
                    "isAvailable": false
                },
                "pdf": {
                    "isAvailable": false
                },
                "webReaderLink": "http://play.google.com/books/reader?id=TpuJxgEACAAJ&hl=&printsec=frontcover&source=gbs_api",
                "accessViewStatus": "NONE",
                "quoteSharingAllowed": false
            },
            "searchInfo": {
                "textSnippet": "In the small north Florida town of Seabrook, a young lawyer named Keith Russo was shot dead at his desk as he worked late one night."
            }
        }
    ]
}

Below is how I try to access:

const volumeInfo.authors = body.items[0].volumeInfo.authors;

The nodejs will crash under body.items[0] when I run it. I also try to get the keys underneath "items" but it will flag errors.

Upvotes: 0

Views: 85

Answers (1)

assax24
assax24

Reputation: 171

Have you tried parsing the response into JSON using JSON.parse method?

Upvotes: 1

Related Questions