Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including goodreads-json-api with all npm packages installed. Try it out:

var goodreadsJsonApi = require("goodreads-json-api")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

goodreads-json-api v1.0.4

Library to get Goodreads API response in JSON format

goodreads-json-api

Library to get Goodreads API response in JSON format

Project scope

The reason I build this project, was because I needed a way to get data from Goodreads API as json. And most of the API returns their data as XML.

This packages don't call goodreads apis internally, it just takes the XML response and returns the parse JSON object. For xml parsing, I've used cheerio project.

Usage

Install the package

npm install --save goodreads-json-api
const goodReadsJSONResponse = require(goodreads-json-api);

https.get('https://www.goodreads.com/book/isbn/0441172717?key=Uxb0zPb86N4STVy2ECWYA', (res) => {
    const options = {
        xml: {
            normalizeWhitespace: true
        }
    }
    const statusCode = res.statusCode;
    const contentType = res.headers['content-type'];
    let error;
    if (statusCode !== 200) {
        error = new Error('Request Failed.\n' +
            `Status Code: ${statusCode}`);
    }
    if (error) {
        console.log(error.message);
        // consume response data to free up memory
        res.resume();
        return;
    }

    res.setEncoding('utf8');
    let rawData = '';
    res.on('data', (chunk) => rawData += chunk);
    res.on('end', () => {
        try {
            const resp = goodReadsJSONResponse.convertToJson(rawData);
            console.log(resp)
        } catch (e) {
            console.log(e.message);
        }
    });
}).on('error', (e) => {
    console.log(`Got error: ${e.message}`);
});

Run npm run demo from this project to see the demo file running.

Contribute

I would greatly appreciate if you can also contribute to this project in any way.

Currently this converts following xml nodes of Goodreads response:

  • author
  • books
  • similiar_books
  • request
  • popular_shelves

For every node, we've a transformer, which you can find here. These transformers are simple json which converts xml to json. You can add any other node, which I've missed here and create a PR.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free