Skip to content

fileReadJSONBuffers

Prefer reading JSON files as buffers when using JSON.parse for better performance.

✅ This rule is included in the node stylistic and stylisticStrict presets.

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.

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.

@paramtext A valid JSON string.

@paramreviver 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.

parse
(await
const fs: any
fs
.
any
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.

@paramtext A valid JSON string.

@paramreviver 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.

parse
(await
const fs: any
fs
.
any
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.

@paramtext A valid JSON string.

@paramreviver 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.

parse
(
const fs: any
fs
.
any
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.

@paramtext A valid JSON string.

@paramreviver 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.

parse
(
const fs: any
fs
.
any
readFileSync
("./settings.json", {
encoding: string
encoding
: "utf-8" }),
);

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.