How to use the writeJson function from fs-promise

Find comprehensive JavaScript fs-promise.writeJson code examples handpicked from public code repositorys.

fs-promise.writeJson is a function in the fs-promise library that writes a JSON object to a file.

132
133
134
135
136
137
138
139
140
141

return fs
  .readJson(filePath)
  .then(function (json) {
    set(json, jsonPath, value);
    return fs.writeJson(filePath, json);
  })
  .catch(function (error) {
    const json = {};
    set(json, jsonPath, value);
fork icon14
star icon17
watch icon2

+ 643 other calls in file

339
340
341
342
343
344
345
346
347
348
async function writeAt(filePath, jsonPath, value) {
  mkdirp.sync(getDirName(filePath));
  try {
    const json = await fsPromise.readJson(filePath);
    set(json, jsonPath, value);
    return await fsPromise.writeJson(filePath, json);
  } catch (error) {
    logSomething('error', 'writeAt', error, 'unknown', null);
    console.log(error)
    // eslint-disable-next-line   
fork icon0
star icon4
watch icon1

+ 593 other calls in file

How does fs-promise.writeJson work?

fs-promise.writeJson is a method in the fs-promise library that allows for the writing of a JSON object to a file using promises, while automatically converting it to a string and formatting it with indentation.

400
401
402
403
404
405
406
407
408
409
mkdirp.sync(getDirName(filePath));

try {
  const json = await fs.readJson(filePath);
  set(json, jsonPath, value);
  return await fs.writeJson(filePath, json);
} catch (error) {
  // eslint-disable-next-line   
  const json_1 = {};
  set(json_1, jsonPath, value);
fork icon0
star icon4
watch icon1

+ 727 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const fsp = require("fs-promise");

const data = {
  name: "John Doe",
  age: 30,
  email: "johndoe@example.com",
};

fsp
  .writeJson("data.json", data)
  .then(() => console.log("Data written to file"))
  .catch((err) => console.error(err));

This example writes the data object to a file called data.json using fs-promise.writeJson(). The method returns a promise, which is used to log a success message to the console if the write operation succeeds, or log an error message if it fails.