npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

axios-get-json-response

v1.0.1

Published

A small utility for validating an axios response and parsing it as JSON, *with* distinguishable error types

Downloads

86

Readme

axios-get-json-response

A simple utility for checking status codes and parsing JSON data from Axios responses. While Axios can do this by itself, it doesn't document any distinguishable error types, and so when an error occurs you can never know what went wrong.

This library fixes that, by giving you two distinct error types. Hopefully this functionality eventually makes it into Axios itself.

Both error types correctly inherit from Error, and therefore a) have stacktraces, and b) can be used with instanceof as well as things based on it, like Bluebird's filtered .catch.

Examples

An existent and valid URL:

"use strict";

const axios = require("axios");
const Promise = require("bluebird");
const getJson = require("axios-get-json-response");

Promise.try(() => {
	return axios.get("http://cryto.net/test.json", getJson.axiosConfiguration); // {"hello": "world"}
}).then((response) => {
	let parsedJson = getJson.parse(response);

	console.log(parsedJson); // { hello: 'world' }
});

Using axios.create to set the custom configuration as a default, and showing the result for an existent URL that is not valid JSON:

"use strict";

const axios = require("axios");
const Promise = require("bluebird");
const getJson = require("axios-get-json-response");

let manualAxios = axios.create(getJson.axiosConfiguration);

Promise.try(() => {
	return manualAxios.get("http://cryto.net/invalid.json"); // {"hello": "world"
}).then((response) => {
	let parsedJson = getJson.parse(response);
	/* throws: ParsingFailed: Could not parse response body as JSON */

	console.log(parsedJson);
});

The result for a non-existent URL:

"use strict";

const axios = require("axios");
const Promise = require("bluebird");
const getJson = require("axios-get-json-response");

let manualAxios = axios.create(getJson.axiosConfiguration);

Promise.try(() => {
	return manualAxios.get("http://cryto.net/non-existent.json"); // URL does not exist
}).then((response) => {
	let parsedJson = getJson.parse(response);
	/* throws: BadResponseCode: Got an unexpected HTTP status code (404) */

	console.log(parsedJson);
});

API

getJson.axiosConfiguration

Some preset configuration options, that disable response code checking and body parsing in Axios. Needed for this library to work. These options are fed directly into Axios; see the example.

getJson.parse(response, [options])

Validates the status code and parses the response body. Returns the parsed JSON if successful, or throws either a BadStatusCode or ParsingFailed error depending on what went wrong (see below).

  • response: The Axios response object to handle.
  • options: Optional.
    • validateStatus: Optional. A custom status code validation function, like in Axios. Defaults to the same as in Axios (all 2XX are considered valid). Expected to return true for a valid/expected status code, and false for an unexpected one.

getJson.BadStatusCode

An error type that signifies that an unexpected status code was received.

Extra properties on this error object:

  • statusCode: The received status code.

getJson.ParsingFailed

An error type that signifies that the response body could not be parsed as JSON.

Changelog

1.0.1 (August 30, 2019)

  • Removed erroneous console.log.

1.0.0 (August 30, 2019)

Initial release.