Skip to main content

JSON Builder

JSON provides methods for stringify, parsing, and working with JSON data, including writing JSON data to a file and reading JSON data from a file.

API Reference

JsonBuilder

Kind: Exported class

Example Basic usage:

import { Builder } from "@ecoflow/utils";

const jsonString = Builder.JSON.stringify({
hello: "world",
});

const parsedJSON = Builder.JSON.parse(jsonString);

console.log(jsonString);
console.log(parsedJSON);

Stringify

stringify(value,[replacer],[space])string

Returns a JSON string representation of the object.

Example Basic usage:

import { Builder } from "@ecoflow/utils";

const jsonString = Builder.JSON.stringify({
hello: "world",
});

console.log(jsonString);

Return: A JSON string representation of the object.

Available arguments :

ParameterTypeDescription
valueanyEnable/Disable verbose.
[replacer](number | string)[] | null | (this: any, key: string, value: any) => anyFunction or array of string or number to perform replace
[space]string | numberAdds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

Parse

parse(value,[reviver])any

Parses a JSON string and returns the parsed result.

Example Basic usage:

import { Builder } from "@ecoflow/utils";

const jsonString = '{"hello":"world"}';
const parsedJSON = Builder.JSON.parse(jsonString);

console.log(parsedJSON);

Return: A JSON object representation of the string.

Available arguments :

ParameterTypeDescription
valuestringString JSON data that you want to parse into a object
[reviver](this: any, key: string, value: any) => anyA function that transforms the results.

Save to file

toFile(filePath, value, options, [replacer], [space])void

Parses a JSON string and returns the parsed result.

Example Basic usage:

import { Builder } from "@ecoflow/utils";

Builder.JSON.toFile(__dirname + "/file.json", {
hello: "world",
});

Return: A JSON object representation of the string.

Available arguments :

ParameterTypeDescription
filePathstringJSON file path to save the data to.
valuestringString JSON data that you want to parse into a object
optionsJsonToFileOptionsFile save options. Default : { recursive: false, mode: "none" }
[replacer](number | string)[] | null | (this: any, key: string, value: any) => anyFunction or array of string or number to perform replace
[space]string | numberAdds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

Parse from file

fromFile(filePath,[reviver])any

Reads a JSON file from a specified path and parses its content.

Example Basic usage:

import { Builder } from "@ecoflow/utils";

const parsedJSON = Builder.JSON.fromFile(__dirname + "/file.json");

console.log(parsedJSON);

Return: A JSON object representation from json file.

Available arguments :

ParameterTypeDescription
filePathstringJSON file path to read from.
[reviver](this: any, key: string, value: any) => anyA function that transforms the results.

TypeScript Properties

JsonToFileOptions

interface JsonToFileOptions {
/** Create directory recursive if not exists. */
recursive?: boolean;

/** Directory create mode*/
mode?: "overwrite" | "append" | "none";
}