GithubHelp home page GithubHelp logo

node-jsonfile's Introduction

Node.js - jsonfile

Easily read/write JSON files in Node.js. Note: this module cannot be used in the browser.

npm Package linux build status windows Build status

Standard JavaScript

Why?

Writing JSON.stringify() and then fs.writeFile() and JSON.parse() with fs.readFile() enclosed in try/catch blocks became annoying.

Installation

npm install --save jsonfile

API


readFile(filename, [options], callback)

options (object, default undefined): Pass in any fs.readFile options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If JSON.parse throws an error, pass this error to the callback. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file, function (err, obj) {
  if (err) console.error(err)
  console.dir(obj)
})

You can also use this method with promises. The readFile method will return a promise if you do not pass a callback function.

const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file)
  .then(obj => console.dir(obj))
  .catch(error => console.error(error))

readFileSync(filename, [options])

options (object, default undefined): Pass in any fs.readFileSync options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If an error is encountered reading or parsing the file, throw the error. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'

console.dir(jsonfile.readFileSync(file))

writeFile(filename, obj, [options], callback)

options: Pass in any fs.writeFile options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, function (err) {
  if (err) console.error(err)
})

Or use with promises as follows:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj)
  .then(res => {
    console.log('Write complete')
  })
  .catch(error => console.error(error))

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
  if (err) console.error(err)
})

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
  if (err) console.error(err)
})

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {
  if (err) console.log(err)
})

appending to an existing JSON file:

You can use fs.writeFile option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
  if (err) console.error(err)
})

writeFileSync(filename, obj, [options])

options: Pass in any fs.writeFileSync options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj)

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2 })

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })

appending to an existing JSON file:

You can use fs.writeFileSync option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { flag: 'a' })

License

(MIT License)

Copyright 2012-2016, JP Richardson [email protected]

node-jsonfile's People

Contributors

ajbogh avatar asingh04 avatar bbbrrriiiaaannn avatar bendingbender avatar edwardbetts avatar ffissore avatar jprichardson avatar kartik2406 avatar kuy avatar linusu avatar max-arnold avatar maxkorp avatar pdehaan avatar peterdavehello avatar r-murphy avatar richdunajewski avatar ryanzim avatar sampathsris avatar wtgtybhertgeghgtwtg 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-jsonfile's Issues

Error callback when null filename sent in jsonfile.readfile

Using jsonfile in my project, I found that in read file if you try to send a null filename it doesn't return an error but throw an exception that exit from app. More precisely it is an fs issue / choice, anyway it wouldn't better if jsonfile returns an error from callback if null input filename is sent?
Here the test :

  describe('> when null JSON file name sent in read', function () {
    it('should send an error if null is sent on filename', function (done) {
      jf.readFile(null, function (err, obj2) {
        assert(err instanceof Error)
        done()
      })  
    })  
  }) 

A solution could be the following:

 function readFile (file, options, callback) {
+  file = file || ''
   if (callback == null) {
     callback = options
     options = {}
   ....

Simply add file = file || '' when readFile starts
Thanks !

updateFile and updateFileSync support

Please let me know if you think it is reasonable to add these two functions or no need for them 😄

  • updateFile(filepath, obj, cb)
  • updateFileSync(filepath, obj)

They read the file, update the file contents with obj using something like Object.assign(), then write back the updated contents to the same file.

Error when running on windows

Hello, I ran my code on my Mac and it worked fine, but when I send it to my friend who runs windows, he gets the following error:

imgpsh_fullsize

I tried doing a self diagnosis, and found the following issue with a related error: #40

Could node-fs-extra help?

The code in question writes to a directory with dynamically created files (I'm depending on jsonfile to create the files), so I assume fs-extra's outputJson will fix the whole issue, correct?

Thank you,

-- Egoscio

Unexpected end of json input file on read

Sometimes jsonfile gives me:
Unexpected end of JSON input
on a correct file.
If I try to read more times I have almost always the correct parsing, but sometimes it gives this error.

I'm not able to understand when it happens, anyway as soon as I can I'll try some stress test on some file with this behavior ... for example, a type of file is this:

[ {"repoName":"DSP_Projects","labName":"dsp 1","state":"STOPPED"},
  {"repoName":"test_repo","labName":"networkTestDir","state":"STOPPED"},
  {"repoName":"test_repo","labName":"networkTestDir2","state":"STOPPED"},
  {"repoName":"test_repo","labName":"runningDelete","state":"RUNNING"},
  {"repoName":"test_repo","labName":"newLab","state":"STOPPED"},
  {"repoName":"test_repo","labName":"toDelete","state":"STOPPED"},
  {"repoName":"test_repo","labName":"anotherNetwork","state":"STOPPED"},
  {"repoName":"test_repo","labName":"existentLab","state":"NO_NETWORK"},
  {"repoName":"test_repo","labName":"toEditLab","state":"STOPPED"},
  {"repoName":"test_repo","labName":"MITM","state":"STOPPED"},
  {"repoName":"DSP_Projects","labName":"first","state":"STOPPED"},
  {"repoName":"DSP_Projects","labName":"second","state":"STOPPED"},
  {"repoName":"actions_test_repo","labName":"TelnetClientSniffing_1","state":"STOPPED"},
  {"repoName":"actions_test_repo","labName":"ErrorSameSubnet","state":"STOPPED"},
  {"repoName":"actions_test_repo","labName":"ErrorSamePort","state":"STOPPED"}
]

but it happens even with files previously written with jsonfile.

can't install node jsonfile

Hello, i have a project with angular2 beta then i want to overwrite some json file in javascript then i found node-jsonfile, the problem is i can't install it and too i want to know a simple usage.
when i put npm install jsonfile il have this error :

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "install" "jsonfile"
npm ERR! node v6.2.1
npm ERR! npm  v3.9.3
npm ERR! code ENOSELF

npm ERR! Refusing to install jsonfile as a dependency of itself
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     https://github.com/npm/npm/issues

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\ADMyNE\Desktop\node-jsonfile-master\npm-debug.log

thanks

Add a final newline

It is a good practice on most UNIX systems to add a final newline at the end of text files like .json. EOL should be treated as "line terminator", not "line separator". Git diff prints an extra message for files that are missing the last newline character.

I am asking jsonfile to conform to this convention and add a trailing newline to the files produced.

I am happy to submit a patch. Since this change can be considered as controversial, I'd rather wait with my work until there is an agreement that such patch will be accepted.

Related reading:

console.log(err)

in your example you have console.log(err) in the callback from writeFile, but maybe it should be

if (err) throw err; ?

Linux: Hidden file not working

I would like to read an hidden file (starting with a dot).

When I display err I have this : [SyntaxError: ./.gitcommitsummary: Unexpected token t].

Here's the code I tried :

jsonfile.readFile('./.gitcommitsummary', (err, obj) => {
    console.log(err);
    if (err) {
        console.log('Create a file called ".gitcommitsummary" at the root of your project to use git-commit-summary.');
        return;
    }

    console.log(obj);
});

Am I missing something ?

writefile callback if no error?

Hi!
Do You know if the writefile function "callback" is called even on no error?
I find it is called with "err" set to null.
I do not understand if there was an error or not.
I searched for it but it is not written anywhere.
Thank you!
Camillo

`jsonfile.spaces` should default to `null`

jsonfile is basically a wrapper to JSON with additional functionality from fs. Thus, it should show the same behavior as JSON. When JSON.stringify() is not supplied with an argument for spaces, it will not beautify the JSON output. Similarly, jsonfile should default to not beautifying the JSON output.

We can achieve this by defaulting jsonfile.spaces to null.

Add updateFile and updateFileSync methods

Instead of needing to call readFile, modify the Json, and later call writeFile, add an updateFile method that accept a literal object and use it's attributes and hierarchy to update the content of the Json file. To remove entries, they can be set to undefined, so JSON.stringify() will ignore them later.

Throws even if throws is set to false

jsonfile.readFileSync(statePath, { throws: false })

fs.js:521
  if (typeof m === 'string')
               ^
Error: ENOENT: no such file or directory, open '/.../balance-reporting.json'

obj.data ? How do I read specific element in json file?

Okay but what if it's in the format :

{
"data": [
{
"email": " ",
"products": [ ],
"username": " "
},
{
"email": " ",
"products": [
{
"actual_food_amount": "15",
"amount": "20",
"drink": "Local Drink ",
"food": "Dasheen meal w/ codfish",
"id": 2,
"location": "Orchard Restaurant"
}
]
}
}
and I want to read "food"

jsonfile append

I am trying to append the json file but it is creating file like this

{"id":1, name: "abc"}
{"id":2, name: "cde"}
{"id":3, name: "fgh"}

so in reading json file it is creating error.

Is there anyway to create file like this ?

{[
{"id":1, name: "abc"},
{"id":2, name: "cde"},
{"id":3, name: "fgh"}
]}

Failed to read application data TypeError: content.replace is not a function

I got an error with my setup (Webpack + Babel + Electron). Here is a full backtrace.

Failed to read application data TypeError: content.replace is not a function
    at stripBom (/Users/kodama/Work/fika-electron/dist/main.bundle.js:229:22)
    at Object.readFileSync (/Users/kodama/Work/fika-electron/dist/main.bundle.js:175:14)
    at Object.<anonymous> (/Users/kodama/Work/fika-electron/dist/main.bundle.js:84:32)
    at __webpack_require__ (/Users/kodama/Work/fika-electron/dist/main.bundle.js:20:30)
    at /Users/kodama/Work/fika-electron/dist/main.bundle.js:40:18
    at Object.<anonymous> (/Users/kodama/Work/fika-electron/dist/main.bundle.js:43:10)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)

In the process of debugging node-jsonfile module, I figured out the error is raised at following line (sourcecode).

function stripBom (content) {
  // we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified
  if (Buffer.isBuffer(content)) content = content.toString('utf8') // THIS LINE
  content = content.replace(/^\uFEFF/, '')
  return content
}

In the line, Buffer.isBuffer(...) returns false even if content is Buffer instance. Because Buffer.isBuffer(...) API relies on instanceof operator for checking type (sourcecode), we can use typeof operator for increasing robustness and workaround.

if (typeof content !== 'string') content = content.toString('utf8')

By the way, when I run a code without Webpack and Babel, it works well. I couldn't determine which layer causes this issue.

Is there anyone struggling same issue?

autocreate folder doesnt work from Path-value

Hi,

is it possible to create folders from path-value if this directory not exist?

function saveJsonInFile(jsonObject, pathToSave) {
    jsonfile.writeFileSync(pathToSave, jsonObject);
}

fs.js:584
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT: no such file or directory, open 'C:\workspace\demoNode\out2\chn.json'

Reading of UTF8-encoded (w/ BOM) files fails

This package would be much helper if it abstracts away utf8's BOM handling. Currently reading fails for files encoded in utf8 (JSON.parse throws "Unexpected symbol" at BOM).

Grunt's file module can solve this problem (see below). Why not to reimplement the similar approach here?

file.read = function(filepath, options) {
  if (!options) { options = {}; }
  var contents;
  grunt.verbose.write('Reading ' + filepath + '...');
  try {
    contents = fs.readFileSync(String(filepath));
    // If encoding is not explicitly null, convert from encoded buffer to a
    // string. If no encoding was specified, use the default.
    if (options.encoding !== null) {
      contents = iconv.decode(contents, options.encoding || file.defaultEncoding);
      // Strip any BOM that might exist.
      if (!file.preserveBOM && contents.charCodeAt(0) === 0xFEFF) {
        contents = contents.substring(1);
      }
    }
    grunt.verbose.ok();
    return contents;
  } catch(e) {
    grunt.verbose.error();
    throw grunt.util.error('Unable to read "' + filepath + '" file (Error code: ' + e.code + ').', e);
  }
};

writeFile doesn't work with some json

I tried to write a json like this:

[ live1: { block: '0.007',
activation: '0.005',
rangeB: '0.002',
flashEnd: '0.006' },
live2: { block: '0.007',
activation: '0.005',
rangeB: '0.002',
flashEnd: '0.006' },
live3: { block: '0.007',
activation: '0.005',
rangeB: '0.002',
flashEnd: '0.006' }]

but the output is :
[]

it works if the JSON is like this:
[{ "name": "live1",
"block": "0.007",
"activation": "0.005",
"rangeB": "0.002",
"flashEnd": "0.006"
},{
"name": "live2",
"block": "0.007",
"activation": "0.005",
"rangeB": "0.002",
"flashEnd": "0.006"
},{
"name": "live3",
"block": "0.007",
"activation": "0.005",
"rangeB": "0.002",
"flashEnd": "0.006"
}]

seem read & write sync has sequentially problem

@jprichardson
My flow is

jf.writeFileSync(file_A_path, jsonData)

var data = null;
data =  jf.readFileSync(file_A_path, false)

jf.writeFileSync(file_B_path, data );

After we get 2 file (A, B), then compare both,result is B lacks some object json data compare with A.
if I change to async like below >>

jf.writeFile(file_A_path, jsonData, function(){
    var data = null;
    data =  jf.readFileSync(file_A_path, false)
    jf.writeFileSync(file_B_path, data );
})

Then A and B file data is the same.
So what I wrong here?

Extra characters in written file

Hello JP. Thanks for providing you jsonfile package.
I have been trying to use your jsonfile package and I am having an interesting problem.
This code:
jsonfile.writeFileSync('/tmp/gregg.dat', '{ "type":"configuration/entityTypes/HCP", \n"attributes": {\n');
Writes this:
"{ \"type\":\"configuration/entityTypes/HCP\", \n\"attributes\": {\n”
instead of:
{ "type":"configuration/entityTypes/HCP”, “attributes”: {

Am I doing something wrong?

Shorten the names of read/write functions

readFile and writeFile feels kind of given, since the package is called jsonfile and it's purpose to handle files. The same goes for writeFileSync

Is there any reasoning for why the aren't called just read and write? The whole file suffix seems redundant otherwise.

writeFileSync does not seem to be writing correctly

I'm not 100% sure on the cause seems to be some encoding issue as files written by it on node 6.8.0 and latest version are appeared to be empty to node and notepad++, but Webstorm can read them fine.

This is on windows with node 6.8.0 and latest jsonfile.

Problem in read JSON

Hi, i have this code:
var outputData = jsonfile.readFileSync(outputFile);
console.dir(outputData);

In the outputData one example of content:
id": 768691965814636545,(look this last 4 characters)
"is-retweet": false,
.......

In console.dir(outputData); i get this for the same value:
"id": 768691965814636500,
"is-retweet": false,
.......

In all objects in json, only change the last 4 characters of ID, other attribs works ok

Any idea?

Thanks

Appending to an existing JSON file:

If I do something like this:


var file = 'mods-installed.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
  console.error(err)
})

jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
  console.error(err)
})

If I already have something like this in my JSON file:

{
    "name": "LP"
}

I will get this:

{
    "name": "LP"
} {
    "name": "JP"
} {
    "name": "JP"
}

Is there a way to wrap all this objects in ONE object?

Thanks!

How to use Jsonfile package in Meteor.js App?

Hello,
I want to use your package jsonfile in my meteor.js app, and use meteor package call meteor add meteorhacks:npm from using npm modules inside meteor app.
Do you have some guide line or example for do this?

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.