Skip to content

writeJsonFile ​

Serializes and writes JSON to a file.

Example ​

ts
import { 
SF
} from "@duplojs/server-utils";
const
result
= await
SF
.
writeJsonFile
("/tmp/config.json", {
enabled
: true });
// result: E.Ok | SF.FileSystemLeft<"write-json-file"> await
SF
.
writeJsonFile
("/tmp/config.json", {
enabled
: true }, {
space
: 2 });

Syntax ​

typescript
function writeJsonFile(
  path: string,
  data: unknown,
  params?: {
    space?: number;
  }
): Promise<FileSystemLeft<"write-json-file"> | E.Ok>

Parameters ​

  • path : path of the JSON file.
  • data : data to serialize.
  • params.space : JSON indentation (optional).

Return value ​

  • E.Ok : if the write succeeds.
  • FileSystemLeft<"write-json-file"> : if serialization or writing fails.

Notes ​

  • If JSON.stringify fails, the function returns a failure.

See also ​

Released under the MIT license.