{"id":13493472,"url":"https://github.com/jprichardson/node-jsonfile","last_synced_at":"2025-05-13T19:06:02.541Z","repository":{"id":4610769,"uuid":"5754307","full_name":"jprichardson/node-jsonfile","owner":"jprichardson","description":"Easily read/write JSON files.","archived":false,"fork":false,"pushed_at":"2024-07-26T13:23:54.000Z","size":129,"stargazers_count":1208,"open_issues_count":6,"forks_count":355,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-04-28T00:16:44.663Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jprichardson.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-09-10T19:02:27.000Z","updated_at":"2025-04-21T07:59:22.000Z","dependencies_parsed_at":"2024-09-16T21:07:57.625Z","dependency_job_id":"e463d8a8-5cfd-4729-a62c-035fc6420a3c","html_url":"https://github.com/jprichardson/node-jsonfile","commit_stats":{"total_commits":152,"total_committers":26,"mean_commits":5.846153846153846,"dds":"0.42105263157894735","last_synced_commit":"17f97e14ef930c98aceb0ce2046d98ee9b09a59b"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprichardson%2Fnode-jsonfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprichardson%2Fnode-jsonfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprichardson%2Fnode-jsonfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jprichardson%2Fnode-jsonfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jprichardson","download_url":"https://codeload.github.com/jprichardson/node-jsonfile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252305185,"owners_count":21726616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T19:01:15.566Z","updated_at":"2025-05-13T19:06:02.508Z","avatar_url":"https://github.com/jprichardson.png","language":"JavaScript","readme":"Node.js - jsonfile\n================\n\nEasily read/write JSON files in Node.js. _Note: this module cannot be used in the browser._\n\n[![npm Package](https://img.shields.io/npm/v/jsonfile.svg?style=flat-square)](https://www.npmjs.org/package/jsonfile)\n[![linux build status](https://img.shields.io/github/actions/workflow/status/jprichardson/node-jsonfile/ci.yml?branch=master)](https://github.com/jprichardson/node-jsonfile/actions?query=branch%3Amaster)\n[![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-jsonfile/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-jsonfile/branch/master)\n\n\u003ca href=\"https://github.com/feross/standard\"\u003e\u003cimg src=\"https://cdn.rawgit.com/feross/standard/master/sticker.svg\" alt=\"Standard JavaScript\" width=\"100\"\u003e\u003c/a\u003e\n\nWhy?\n----\n\nWriting `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.\n\n\n\nInstallation\n------------\n\n    npm install --save jsonfile\n\n\n\nAPI\n---\n\n* [`readFile(filename, [options], callback)`](#readfilefilename-options-callback)\n* [`readFileSync(filename, [options])`](#readfilesyncfilename-options)\n* [`writeFile(filename, obj, [options], callback)`](#writefilefilename-obj-options-callback)\n* [`writeFileSync(filename, obj, [options])`](#writefilesyncfilename-obj-options)\n\n----\n\n### readFile(filename, [options], callback)\n\n`options` (`object`, default `undefined`): Pass in any [`fs.readFile`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback) options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).\n  - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.\n  If `false`, returns `null` for the object.\n\n\n```js\nconst jsonfile = require('jsonfile')\nconst file = '/tmp/data.json'\njsonfile.readFile(file, function (err, obj) {\n  if (err) console.error(err)\n  console.dir(obj)\n})\n```\n\nYou can also use this method with promises. The `readFile` method will return a promise if you do not pass a callback function.\n\n```js\nconst jsonfile = require('jsonfile')\nconst file = '/tmp/data.json'\njsonfile.readFile(file)\n  .then(obj =\u003e console.dir(obj))\n  .catch(error =\u003e console.error(error))\n```\n\n----\n\n### readFileSync(filename, [options])\n\n`options` (`object`, default `undefined`): Pass in any [`fs.readFileSync`](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).\n- `throws` (`boolean`, default: `true`). If an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.\n\n```js\nconst jsonfile = require('jsonfile')\nconst file = '/tmp/data.json'\n\nconsole.dir(jsonfile.readFileSync(file))\n```\n\n----\n\n### writeFile(filename, obj, [options], callback)\n\n`options`: Pass in any [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback) options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). 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.\n\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFile(file, obj, function (err) {\n  if (err) console.error(err)\n})\n```\nOr use with promises as follows:\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFile(file, obj)\n  .then(res =\u003e {\n    console.log('Write complete')\n  })\n  .catch(error =\u003e console.error(error))\n```\n\n\n**formatting with spaces:**\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {\n  if (err) console.error(err)\n})\n```\n\n**overriding EOL:**\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFile(file, obj, { spaces: 2, EOL: '\\r\\n' }, function (err) {\n  if (err) console.error(err)\n})\n```\n\n\n**disabling the EOL at the end of file:**\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {\n  if (err) console.log(err)\n})\n```\n\n**appending to an existing JSON file:**\n\nYou can use `fs.writeFile` option `{ flag: 'a' }` to achieve this.\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/mayAlreadyExistedData.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {\n  if (err) console.error(err)\n})\n```\n\n----\n\n### writeFileSync(filename, obj, [options])\n\n`options`: Pass in any [`fs.writeFileSync`](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options) options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). 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.\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFileSync(file, obj)\n```\n\n**formatting with spaces:**\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFileSync(file, obj, { spaces: 2 })\n```\n\n**overriding EOL:**\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\\r\\n' })\n```\n\n**disabling the EOL at the end of file:**\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/data.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })\n```\n\n**appending to an existing JSON file:**\n\nYou can use `fs.writeFileSync` option `{ flag: 'a' }` to achieve this.\n\n```js\nconst jsonfile = require('jsonfile')\n\nconst file = '/tmp/mayAlreadyExistedData.json'\nconst obj = { name: 'JP' }\n\njsonfile.writeFileSync(file, obj, { flag: 'a' })\n```\n\nLicense\n-------\n\n(MIT License)\n\nCopyright 2012-2016, JP Richardson  \u003cjprichardson@gmail.com\u003e\n","funding_links":[],"categories":["JavaScript","Repository","Uncategorized"],"sub_categories":["Object / JSON / JSON Schema","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjprichardson%2Fnode-jsonfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjprichardson%2Fnode-jsonfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjprichardson%2Fnode-jsonfile/lists"}