{"id":16148993,"url":"https://github.com/zardoy/modify-json-file","last_synced_at":"2026-01-21T02:02:46.931Z","repository":{"id":71106961,"uuid":"375329824","full_name":"zardoy/modify-json-file","owner":"zardoy","description":"Simple module for modifying fields in JSON files","archived":false,"fork":false,"pushed_at":"2021-11-20T17:23:32.000Z","size":303,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"next","last_synced_at":"2025-09-21T21:43:21.568Z","etag":null,"topics":["json","json-file","jsonc","modify-json","nodejs","package-json"],"latest_commit_sha":null,"homepage":"http://npmjs.com/modify-json-file","language":"TypeScript","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/zardoy.png","metadata":{"files":{"readme":"readme.md","changelog":null,"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":"2021-06-09T11:23:40.000Z","updated_at":"2023-08-31T07:04:59.000Z","dependencies_parsed_at":"2023-02-24T19:30:13.323Z","dependency_job_id":null,"html_url":"https://github.com/zardoy/modify-json-file","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/zardoy/modify-json-file","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zardoy%2Fmodify-json-file","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zardoy%2Fmodify-json-file/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zardoy%2Fmodify-json-file/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zardoy%2Fmodify-json-file/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zardoy","download_url":"https://codeload.github.com/zardoy/modify-json-file/tar.gz/refs/heads/next","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zardoy%2Fmodify-json-file/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28622472,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T23:49:58.628Z","status":"online","status_checked_at":"2026-01-21T02:00:08.227Z","response_time":86,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["json","json-file","jsonc","modify-json","nodejs","package-json"],"created_at":"2024-10-10T00:35:53.371Z","updated_at":"2026-01-21T02:02:46.915Z","avatar_url":"https://github.com/zardoy.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Modify JSON\n\nSimplest way to modify JSON files\n\n[API](https://paka.dev/npm/modify-json-file)\n\n## Why?\n\nBecaues I got tired of writing `read`/`write` functions for JSON files, especially when I need to change dozens of files.\n\n## Usage\n\n- Only async use\n\n### Basic Example\n\nLet's start with *package.json* file:\n```json\n// 📁package.json\n{\n    \"name\": \"package\",\n    \"main\": \"index.js\",\n    \"author\": \"eldar\",\n    \"files\": [ \"build\" ],\n    \"dependencies\": {\n        \"type-fest\": \"*\",\n        \"fdir\": \"\u003e=2\"\n    }\n}\n```\n\nAnd this code:\n\n```ts\nimport { modifyJsonFile } from \"modify-json-file\";\n\n// modify package.json in the same dir\nawait modifyJsonFile(\n    path.join(__dirname, \"package.json\"),\n    {\n        name: s =\u003e `super ${s}`,\n        main: \"build/electron.js\",\n        files: undefined, // removing the property\n        dependencies: {\n            \"type-fest\": \"^1.0.0\"\n        }\n    }\n)\n```\n\nAfter running the code, *package.json* will be:\n\n```json\n{\n    \"name\": \"super package\",\n    \"main\": \"build/electron.js\",\n    \"author\": \"eldar\",\n    \"dependencies\": {\n        \"type-fest\": \"^1.0.0\"\n    }\n}\n```\n\nAs you can see above, `modifyJsonFile` only merges **only on top level** properties. Currently, there is no support for nested property merging.\n\nNote that to simplify docs I won't use `path` module here, but you should use it everywhere.\n\n\u003e In example above, `modifyPackageJson` should be used to provider typing for you\n\nAs always, for more usage examples you can look into `test-d/index.test-d.ts` or `tests` files.\n\n### Non-object root value\n\n\u003e Remember, that at root level value can be any valid JSON value: `string`, `number`, `boolean`, `null`, `object` or `array`.\n\nBe aware of modifying non object JSON files (where root type is not an object). For example:\n\nOur code:\n\n```ts\nimport { modifyJsonFile } from \"modify-json-file\";\n\n// telling that root type is number (in this case it's obligatory)\nawait modifyJsonFile\u003cnumber\u003e(\"package.json\", n =\u003e n + 1);\n```\n\nExpected JSON:\n\n```json\n// 📁someNumber.json\n5\n```\n\nActual JSON:\n\n```json\n// 📁someNumber.json\n{\n    \"retries\": 5\n}\n```\n\nAfter running the code above, without any warnings you will get this:\n\n```json\n// 📁someNumber.json\n\"[object Object]1\"\n```\n\nThat's because callback `n =\u003e n + 1` has transformed `n` (object) into string.\n\nHere, despite of the TS type (number), `n` is object in runtime, so `n + 1` just stringified `n` and returned `[object Object]1`.\nThen this module just stringified the string to store output as valid JSON string in file.\n\nRemember, **this module doesn't do any type checking in runtime**, you need to use `typeof` in callback for checking root types or schema validators (like [ajv](http://npmjs.com/ajv)) for objects.\n\n### Formatting\n\nBy default, it will preserve tab size (thanks to [detect-indent](https://www.npmjs.com/package/detect-indent)), but you can control this by overriding `tabSize` option. For example, we can use it to just format the file:\n\n```ts\nimport { modifyJsonFile } from \"modify-json-file\";\n\n// this will format file to use \\t (hard tabs)\nawait modifyJsonFile(\"someFile.json\", {}, { tabSize: \"hard\" });\n```\n\n## JSONC\n\nPass `{ removeJsonc: true }` option to enable processing `jsonc` files.\n\n**WARNING**, this option will remove comments and trailing commas forever!\n\nThis is temporary limitation and the option will be renamed to `jsonc` once limitation is removed.\n\n\u003e `modifyTsConfigJsonFile` has this option enabled by default\n\n## TODO\n\nDocs:\n\n- [ ] Custom parser / stringifer (look at quicktype issue) to save property order\n- [ ] Helper exports section\n- [ ] Extend package.json typings\n- [ ] Examples with immer\n- [ ] Fix auto generated docs\n- [ ] Runtypes?\n- [ ] Describe all possible usage cases\n- [ ] Give a hint, that it doesn't perform schema checking again actual file contents when type is passed into generic function `modifyJsonFile`\n\n- [ ] Strip bom option\n\n```ts\nawait fs.promises.readFile(./package.json, \"utf8\");\n```\n\nInto this:\n\n```ts\nawait fs.promises.readFile(path.join(__dirname, \"package.json\"), \"utf8\");\n```\n\n- [ ] find a way to use FS in builder-way (like [fdir](https://www.npmjs.com/package/fdir) does) and deprecate this module\n\n## Related\n\n\u003c!-- With *jsonfile*, you need to read / write objects. 1 function is simpler. That's super important for me, because I need to work with JSON files a lot. --\u003e\n\n- [jsonfile](https://npmjs.com/jsonfile): simple reading / writing for json files\n- immer ?\n\n[Other cool modules for working with JSON](https://github.com/search?q=user%3Asindresorhus+json)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzardoy%2Fmodify-json-file","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzardoy%2Fmodify-json-file","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzardoy%2Fmodify-json-file/lists"}