Default JSON Parser in Axios HTTP Client
This article explains the default JSON parsing mechanism used internally by the Axios HTTP client. It details the underlying parser responsible for serializing and deserializing data, how Axios processes incoming JSON payloads by default, and how developers can override this behavior when standard parsing does not meet specific application requirements.
The Default Parser: Native JSON.parse()
Axios relies on the standard JavaScript JSON.parse()
method as its default internal JSON parser. It does not include or
depend on any third-party JSON parsing libraries.
When a response is received from a server, Axios executes a series of
default transform functions defined in
axios.defaults.transformResponse. The standard
transformation checks if the incoming data is a string and attempts to
parse it using JSON.parse(). If parsing succeeds, the
resulting JavaScript object is assigned to the
response.data property; if parsing fails (for example, if
the payload is plain text or HTML), Axios catches the syntax error
silently and returns the raw string data untouched.
How Axios Implements JSON Parsing Internally
Under the hood, Axios configures its default transformation in the following manner:
transformResponse: [
function transformResponse(data) {
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) {
/* Ignore error and return data as-is */
}
}
return data;
}
]This behavior ensures that JSON responses are automatically converted into usable JavaScript objects without requiring manual deserialization on every request.
Limitations of the Default Parser
Because Axios relies strictly on JSON.parse(), it
inherits all the standard limitations of native JavaScript JSON
handling:
- Large Integers (BigInt): Long 64-bit integers
(e.g., Twitter IDs, Snowflake IDs) exceed JavaScript's
Number.MAX_SAFE_INTEGERand will lose precision during parsing. - Date Deserialization: Date strings formatted in ISO
8601 remain plain strings and are not automatically converted into
native
Dateinstances.
Customizing the Default JSON Parser
Developers can replace the default JSON.parse()
implementation with specialized libraries (such as
lossless-json or json-bigint) by overriding
the transformResponse configuration globally or per
request:
import axios from 'axios';
import JSONbig from 'json-bigint';
const client = axios.create({
transformResponse: [
(data) => {
try {
return JSONbig.parse(data);
} catch (err) {
return data;
}
}
]
});Modifying transformResponse gives full control over how
payloads are deserialized before reaching application logic.