JSON.parse() can parse buffers directly without needing to convert them to strings first.
Reading files as buffers when parsing JSON avoids unnecessary string conversion overhead.
When reading a JSON file with fs.readFile() or fs.readFileSync() and then parsing it with JSON.parse(), there’s no need to specify UTF-8 encoding.
The file can be read as a buffer, and JSON.parse() will handle the buffer directly.
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
Asynchronously reads the entire contents of a file.
@param ― path A path to a file. If a URL is provided, it must use the file: protocol.
If a FileHandle is provided, the underlying file will not be closed automatically.
@param ― options An object that may contain an optional flag.
If a flag is not provided, it defaults to 'r'.
readFile("./package.json", "utf8"));
const
const data:any
data =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
Asynchronously reads the entire contents of a file.
@param ― path A path to a file. If a URL is provided, it must use the file: protocol.
If a FileHandle is provided, the underlying file will not be closed automatically.
@param ― options An object that may contain an optional flag.
If a flag is not provided, it defaults to 'r'.
readFile("./data.json", "utf-8"));
const
const config:any
config =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
Synchronously reads the entire contents of a file.
@param ― path A path to a file. If a URL is provided, it must use the file: protocol.
If a file descriptor is provided, the underlying file will not be closed automatically.
@param ― options Either the encoding for the result, or an object that contains the encoding and an optional flag.
If a flag is not provided, it defaults to 'r'.
readFileSync("./config.json", "utf8"));
const
const settings:any
settings =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
Synchronously reads the entire contents of a file.
@param ― path A path to a file. If a URL is provided, it must use the file: protocol.
If a file descriptor is provided, the underlying file will not be closed automatically.
@param ― options Either the encoding for the result, or an object that contains the encoding and an optional flag.
If a flag is not provided, it defaults to 'r'.
readFileSync("./settings.json", {
encoding: BufferEncoding
encoding: "utf-8" }),
);
const
const packageJson:any
packageJson =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
Asynchronously reads the entire contents of a file.
If no encoding is specified (using options.encoding), the data is returned
as a Buffer object. Otherwise, the data will be a string.
If options is a string, then it specifies the encoding.
When the path is a directory, the behavior of fsPromises.readFile() is
platform-specific. On macOS, Linux, and Windows, the promise will be rejected
with an error. On FreeBSD, a representation of the directory's contents will be
returned.
An example of reading a package.json file located in the same directory of the
running code:
It is possible to abort an ongoing readFile using an AbortSignal. If a
request is aborted the promise returned is rejected with an AbortError:
import { readFile } from'node:fs/promises';
try {
const controller = newAbortController();
const { signal } = controller;
const promise = readFile(fileName, { signal });
// Abort the request before the promise settles.
controller.abort();
awaitpromise;
} catch (err) {
// When a request is aborted - err is an AbortError
console.error(err);
}
Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering fs.readFile performs.
Any specified FileHandle has to support reading.
@since ― v10.0.0
@param ― path filename or FileHandle
readFile("./package.json"));
const
const data:any
data =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
Asynchronously reads the entire contents of a file.
If no encoding is specified (using options.encoding), the data is returned
as a Buffer object. Otherwise, the data will be a string.
If options is a string, then it specifies the encoding.
When the path is a directory, the behavior of fsPromises.readFile() is
platform-specific. On macOS, Linux, and Windows, the promise will be rejected
with an error. On FreeBSD, a representation of the directory's contents will be
returned.
An example of reading a package.json file located in the same directory of the
running code:
It is possible to abort an ongoing readFile using an AbortSignal. If a
request is aborted the promise returned is rejected with an AbortError:
import { readFile } from'node:fs/promises';
try {
const controller = newAbortController();
const { signal } = controller;
const promise = readFile(fileName, { signal });
// Abort the request before the promise settles.
controller.abort();
awaitpromise;
} catch (err) {
// When a request is aborted - err is an AbortError
console.error(err);
}
Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering fs.readFile performs.
Any specified FileHandle has to support reading.
@since ― v10.0.0
@param ― path filename or FileHandle
readFile("./data.json"));
const
const config:any
config =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
For detailed information, see the documentation of the asynchronous version of
this API:
readFile
.
If the encoding option is specified then this function returns a
string. Otherwise it returns a buffer.
Similar to
readFile
, when the path is a directory, the behavior of fs.readFileSync() is platform-specific.
import { readFileSync } from'node:fs';
// macOS, Linux, and Windows
readFileSync('<directory>');
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
// FreeBSD
readFileSync('<directory>'); // => <data>
@since ― v0.1.8
@param ― path filename or file descriptor
readFileSync("./config.json"));
const
const settings:any
settings =
var JSON:JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?:(this:any, key:string, value:any)=> any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
If you explicitly prefer to always describe encoding parameters, this rule might not be for you.
Additionally, if you choose to manually choose which nuanced parsing behavior you prefer, you may disagree with this rule and choose not to enable it.