6.1.0 • Published 3 years ago

jsonfile v6.1.0

Weekly downloads
28,722,649
License
MIT
Repository
github
Last release
3 years ago

Node.js - jsonfile

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

npm Package build status windows Build status

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 jprichardson@gmail.com

fs-extraweb-desktop-helperxcassetscuc-gafarchetype-librarycomponennentteasy-select-rnvuedragdropuploadimagesmysql-import-export@cyber-start/create-cyber-docs@cyber-start/create-cyber-library@cyber-start/create-cyber-project@cyber-start/create-cyber-server@cyber-start/create-cyber-tools@cyber-tools/lib-clireact-native-bluetooth2psious-datakilli8n-react-native-fast-image@3dx900/ultradjsconqueramztezster2@alanibrus/linkedin-pdf-to-jsonrn-send-sms@beyondcoin/beyondasset-block-explorer@ymkz/webappgen-clinode-next-serverkurigashvili-testpackagekurigashvili-testpackage-xeric-test-bingolink-cli@pythonpete32/power-tools@teamsqlio/teamsql-cli@bumble/reloaderrollup-plugin-auto-manifest@starikcetin/git-upm-publisher-2fe-downloaderdatabase-manager-ffselenium-testzenitpseudo-translatify-1pseudo-translatify-2pseudo-translatify-3rjo-pt-test-1kapiltezkateztest1@tapack/testrail-uploader@leafgard/nodegard@nodebug/cucumberemailerkapilteztest2kapilteztest3storybook-addons-playwrightwptp@yushicheng/create-private-lib@cyber-work/create-cyber-work@cyber-work/lib-clibm_scaffold_async_router_testsoldrd-ezdbfake-data-clilidl-lib-address-be@cyber-tools/ns-gitgenerator-hughesglossolalia@sykora/retro-clireact-native-template-rfbasens-frontgit-remote-dgitkonstruct-foremankakuri-ask-clikakuri2-ask-climedici-clikapilkakapiltcloud-archive-s3airscanairscan-examplebb-chatpassitive-cliyijicolorbarget-clean-portcd-svreact-native-esc-pos-sahaab@ihikmawan/tailwindcss-multi-theme-l2@borisovart/atol-kkt-module@frxf/frxf@travisspomer/fluentui-token-pipelinebower-ignorehvxmapperdeneme323112@escape.tech/express@escape.tech/cli@beatjs/create-react-native-moduleows-trongnv-odf@steven-torres/jsxrritmojs-github-graph2ritmojs-github2ritmojs-githubritmojs-github-graph@joseirrazabal/kit@nurimansyah/parcel-plugin-eslint@fundefund/funde_ck@ntt_app/react-native-custom-notification
6.1.0

3 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.0.0

6 years ago

4.0.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.4.0

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.3

9 years ago

2.2.2

9 years ago

2.2.1

9 years ago

2.2.0

9 years ago

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.1

9 years ago

2.0.0

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago

0.0.1

12 years ago