Socket
Book a DemoInstallSign in
Socket

jsonload

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonload

read in a .json file, handles line-delimited files

1.2.0
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

jsonload

read in a json file, handles line-delimited

usage

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)
// 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 parser. You can optionally specify a different parser. It must be an object with a .parse() function:

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):

jsonload('./json-file', { parse: function(obj) {
    console.log(obj);
    return JSON.parse(obj);
} }, function() {});

Keywords

json

FAQs

Package last updated on 19 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.