Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weisjohn/jsonload
read in a json file (supports line-delimited files)
https://github.com/weisjohn/jsonload
Last synced: about 2 months ago
JSON representation
read in a json file (supports line-delimited files)
- Host: GitHub
- URL: https://github.com/weisjohn/jsonload
- Owner: weisjohn
- License: mit
- Created: 2015-07-16T20:19:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-19T10:13:56.000Z (about 9 years ago)
- Last Synced: 2024-11-14T09:32:55.333Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonload
read in a json file, handles line-delimited
### usage
```javascript
var jsonload = require('jsonload');// async
jsonload('./foo', function(err, results) {
console.log(results);
});// sync
var foo = jsonload.sync('./foo');
```### errors
Because `jsonload` handles simple and line-delimited JSON files, there are a three possible errors that may be returned/thrown:
- file not found
- an invalid JSON file
- an invalid JSON line(s)```javascript
// async
jsonload('./foo', function(err, results) {
if (err.code == 'ENOENT')
console.log('file not found');
if (err.name == 'SyntaxError')
console.log('invalid JSON file');
if (err.length)
console.log('invalid JSON lines', e);// on complex files, there may be lines which parsed correctly
console.log(results);
});// sync
try {
var foo = jsonload.sync('./foo');
} catch (err) {
if (err.code == 'ENOENT')
console.log('file not found');
if (err.name == 'SyntaxError')
console.log('invalid JSON file');
if (err.length)
console.log('invalid JSON lines', e);
}
```### parser
By default, `jsonload` utilizes the built-in [`JSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) parser. You can optionally specify a different parser. It must be an object with a `.parse()` function:
```javascript
var EJSON = require('mongodb-extended-json');
var sync = jsonload.sync('./ejson-file', EJSON);jsonload('./ejson-file', EJSON, function(err, ejson) {
console.log(ejson[0]);
});
```The parser is invoked line-by-line. To log each line before parsing (tap-style):
```javascript
jsonload('./json-file', { parse: function(obj) {
console.log(obj);
return JSON.parse(obj);
} }, function() {});
```