How to use the readJson function from fs-promise

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

fs-promise.readJson is a function that reads a JSON file and returns a JavaScript object.

398
399
400
401
402
403
404
405
406
407
408
409


async function writeAt(filePath, jsonPath, value) {
  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   
fork icon0
star icon4
watch icon1

+ 363 other calls in file

337
338
339
340
341
342
343
344
345
346
347
}


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);
fork icon0
star icon4
watch icon1

+ 296 other calls in file

How does fs-promise.readJson work?

fs-promise.readJson is a method from the fs-promise library which reads a JSON file asynchronously and returns a Promise that resolves with the parsed JSON data or rejects with an error if the file cannot be read or parsed. Internally, it uses fs.readFile to read the file and JSON.parse to parse the contents.

Ai Example

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

// Read the content of a JSON file
fsp
  .readJson("/path/to/file.json")
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    console.error(err);
  });

In this example, fs-promise.readJson is used to read the content of a JSON file located at /path/to/file.json. The method returns a promise that resolves with the parsed JSON data if the file exists and its content is valid JSON. If the file does not exist or its content is not valid JSON, the promise is rejected with an error.