Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/read-json-sync
Read and parse a JSON file synchronously
https://github.com/shinnn/read-json-sync
byte-order-mark javascript json nodejs parse synchronous
Last synced: 26 days ago
JSON representation
Read and parse a JSON file synchronously
- Host: GitHub
- URL: https://github.com/shinnn/read-json-sync
- Owner: shinnn
- License: isc
- Created: 2014-10-09T13:58:12.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-12-16T13:00:21.000Z (almost 6 years ago)
- Last Synced: 2024-10-01T10:49:37.143Z (about 1 month ago)
- Topics: byte-order-mark, javascript, json, nodejs, parse, synchronous
- Language: JavaScript
- Size: 73.2 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# read-json-sync
[![npm version](https://img.shields.io/npm/v/read-json-sync.svg)](https://www.npmjs.com/package/read-json-sync)
[![Build Status](https://travis-ci.com/shinnn/read-json-sync.svg?branch=master)](https://travis-ci.com/shinnn/read-json-sync)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/read-json-sync.svg)](https://coveralls.io/github/shinnn/read-json-sync)A [Node.js](https://nodejs.org/) module to read and parse a [JSON](https://json.org/) file synchronously
```javascript
const readJsonSync = require('read-json-sync');readJsonSync('package.json'); //=> {name: 'read-json-sync', version: '1.0.0', ...}
```Node.js built-in [`require`](https://nodejs.org/api/globals.html#globals_require) and [`import`](https://nodejs.org/api/esm.html#esm_interop_with_existing_modules) can do almost the same thing, but this module doesn't [cache](https://nodejs.org/api/modules.html#modules_caching) results.
## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install read-json-sync
```## API
```javascript
const readJsonSync = require('read-json-sync');
```### readJsonSync(*path* [, *options*])
*path*: `string` [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer) [`URL`](https://nodejs.org/api/url.html#url_class_url) (JSON filename) or `integer` (file descriptor)
*options*: `Object` `string` ([`fs.readFile`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback) options or an encoding of the file)
Return: `any` (parsed [JSON](https://tools.ietf.org/html/rfc7159) data)It automatically ignores the leading [byte order mark](https://unicode.org/faq/utf_bom.html).
```javascript
// with-bom.json: '\uFEFF{"a": 1}'JSON.parse('\uFEFF{"a": 1}'); // throws a SyntaxError
readJsonSync('with-bom.json'); //=> {a: 1}
```## License
[ISC License](./LICENSE) © 2017 - 2018 Shinnosuke Watanabe