Code Monkey home page Code Monkey logo

load-json-file's Introduction

load-json-file

Read and parse a JSON file

It also strips UTF-8 byte order mark (BOM).

Install

$ npm install load-json-file

Usage

import {loadJsonFile} from 'load-json-file';

console.log(await loadJsonFile('foo.json'));
//=> {foo: true}

API

loadJsonFile(filePath, options?)

Returns a Promise<unknown> with the parsed JSON.

loadJsonFileSync(filepath, options?)

Returns the parsed JSON.

options

Type: object

beforeParse

Type: Function

Applies a function to the JSON string before parsing.

reviver

Type: Function

Prescribes how the value originally produced by parsing is transformed, before being returned. See the JSON.parse docs for more.

load-json-file for enterprise

Available as part of the Tidelift Subscription.

The maintainers of load-json-file and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Related

load-json-file's People

Contributors

anfedorov avatar arthurvr avatar bendingbender avatar coreyfarrell avatar deniscarriere avatar fisker avatar gakada avatar linusu avatar ntwb avatar realityking avatar richienb avatar samverschueren avatar simenb avatar sindresorhus avatar vikiboss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

load-json-file's Issues

suggestion

Would be nice if this had 0 dependencies:

import {readFileSync} from 'node:fs';
import {readFile} from 'node:fs/promises';

const parse = (uint8, options = {}) => {
  // unlike buf.toString('utf8') or or readFile(path, 'utf8'), TextDecoder will remove BOM
  let data = new TextDecoder().decode(uint8);

  if (typeof options.beforeParse === 'function') {
    data = options.beforeParse(data);
  }

  return JSON.parse(data, options.reviver);
};

export const loadJsonFile = (filePath, options) =>
  readFile(filePath).then(data => parse(data, options));

export const loadJsonFileSync = (filePath, options) =>
  parse(readFileSync(filePath), options);

Error: ENOENT: no such file or directory, open '. /file.json'

It cannot seem to find the files which are there. I was able to require the file I needed as js with the same exact path save for the extension. To be sure the issue was not caused by a file path typo I did the following:

echo "{}" > file.json

Then in the importing file in the same directory:

const loadJsonFile = require('load-json-file');
 
loadJsonFile('./file.json').then(json => {
    console.log(json);
    //=> {foo: true} 
});

which produced the error:
Error: ENOENT: no such file or directory, open '. /file.json'

Error when json file is not found?

Should this module error when the json file is not found, or does this module assume that the file exists?

I have this function:

function test() {
    console.log("test function called");

    loadJsonFile(jsonpath).then(function (json) {
        console.log("json data: ", json);
    })
}

The very first console.log() statement always prints to the console, but the second console.log() statement only prints if the file at jsonpath exists, but if the json file doesn't exist, nothing happens. Should a "file not found" (ENOENT) error be included in the list of helpful errors, or should we be doing our own checks to see if the file exists before calling this module?

BTW: Thanks for all the awesome work you do!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.