{"id":16208468,"url":"https://github.com/ehmicky/truncate-json","last_synced_at":"2025-03-16T11:30:36.545Z","repository":{"id":53848679,"uuid":"519864461","full_name":"ehmicky/truncate-json","owner":"ehmicky","description":"Truncate a JSON string.","archived":false,"fork":false,"pushed_at":"2025-03-13T05:04:08.000Z","size":5139,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-13T06:19:08.351Z","etag":null,"topics":["javascript","json","json-parser","length","library","limit","maximum","nodejs","parse","parsing","serialization","serialize","shortener","size","size-calculation","string","stringify","truncate","types","typescript"],"latest_commit_sha":null,"homepage":"","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/ehmicky.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2022-07-31T19:06:29.000Z","updated_at":"2025-03-13T05:04:12.000Z","dependencies_parsed_at":"2024-08-23T23:34:49.873Z","dependency_job_id":"62a6a7e9-cfa2-4b4d-80dc-16065d53bb66","html_url":"https://github.com/ehmicky/truncate-json","commit_stats":{"total_commits":261,"total_committers":3,"mean_commits":87.0,"dds":"0.019157088122605415","last_synced_commit":"1b6634b5aeb93b707787c9d4967c463484abcd66"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":"ehmicky/template-javascript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Ftruncate-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Ftruncate-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Ftruncate-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Ftruncate-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ehmicky","download_url":"https://codeload.github.com/ehmicky/truncate-json/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243814907,"owners_count":20352037,"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":["javascript","json","json-parser","length","library","limit","maximum","nodejs","parse","parsing","serialization","serialize","shortener","size","size-calculation","string","stringify","truncate","types","typescript"],"created_at":"2024-10-10T10:17:08.015Z","updated_at":"2025-03-16T11:30:36.138Z","avatar_url":"https://github.com/ehmicky.png","language":"JavaScript","readme":"[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js\u0026colorA=404040\u0026logoColor=66cc33)](https://www.npmjs.com/package/truncate-json)\n[![Browsers](https://img.shields.io/badge/-Browsers-808080?logo=firefox\u0026colorA=404040)](https://unpkg.com/truncate-json?module)\n[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript\u0026colorA=404040\u0026logoColor=0096ff)](/src/main.d.ts)\n[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov\u0026colorA=404040)](https://codecov.io/gh/ehmicky/truncate-json)\n[![Minified size](https://img.shields.io/bundlephobia/minzip/truncate-json?label\u0026colorA=404040\u0026colorB=808080\u0026logo=webpack)](https://bundlephobia.com/package/truncate-json)\n[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon\u0026colorA=404040\u0026logoColor=9590F9)](https://fosstodon.org/@ehmicky)\n[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium\u0026colorA=404040)](https://medium.com/@ehmicky)\n\nTruncate a JSON string.\n\n# Hire me\n\nPlease\n[reach out](https://www.linkedin.com/feed/update/urn:li:activity:7117265228068716545/)\nif you're looking for a Node.js API or CLI engineer (11 years of experience).\nMost recently I have been [Netlify Build](https://github.com/netlify/build)'s\nand [Netlify Plugins](https://www.netlify.com/products/build/plugins/)'\ntechnical lead for 2.5 years. I am available for full-time remote positions.\n\n# Examples\n\n```js\nimport truncateJson from 'truncate-json'\n\n// Object properties and array items beyond `maxSize` are omitted.\nconst maxSize = 15\nconst jsonString = JSON.stringify({ a: 'one', b: 'two' })\nconsole.log(jsonString)\n// '{\"a\":\"one\",\"b\":\"two\"}' (21 bytes)\nconsole.log(truncateJson(jsonString, maxSize).jsonString)\n// '{\"a\":\"one\"}' (11 bytes)\n```\n\n```js\n// Works deeply inside objects and arrays\nconst jsonString = JSON.stringify([\n  'one',\n  { a: 'two', b: { c: 'three', d: 'four' } },\n  'five',\n])\nconsole.log(jsonString)\n// '[\"one\",{\"a\":\"two\",\"b\":{\"c\":\"three\",\"d\":\"four\"}},\"five\"]' (55 bytes)\nconst returnValue = truncateJson(jsonString, 40)\nconsole.log(returnValue.jsonString)\n// '[\"one\",{\"a\":\"two\",\"b\":{\"c\":\"three\"}}]' (37 bytes)\n\n// Omitted/truncated properties are returned\nconsole.log(returnValue.truncatedProps)\n// [\n//   { path: [ 1, 'b', 'd' ], value: 'four' },\n//   { path: [ 2 ], value: 'five' }\n// ]\nconst isTruncated = returnValue.truncatedProps.length !== 0\nconsole.log(isTruncated) // true\n```\n\n```js\n// Indentation is automatically detected and preserved\nconst jsonString = JSON.stringify({ a: 'one', b: 'two' }, undefined, 2)\nconsole.log(jsonString)\n// '{\n//   \"a\": \"one\",\n//   \"b\": \"two\"\n// }' (30 bytes)\nconsole.log(truncateJson(jsonString, 25).jsonString)\n// '{\n//   \"a\": \"one\"\n// }' (16 bytes)\n```\n\n```js\n// The top-level value can be any JSON type, not only objects or arrays\nconst jsonString = JSON.stringify('This is an example top-level string')\nconsole.log(truncateJson(jsonString, 25).jsonString)\n// '\"This is an example t...\"' (25 bytes)\n```\n\n# Install\n\n```bash\nnpm install truncate-json\n```\n\nThis package works in both Node.js \u003e=18.18.0 and\n[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist).\n\nThis is an ES module. It must be loaded using\n[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),\nnot `require()`. If TypeScript is used, it must be configured to\n[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),\nnot CommonJS.\n\n# API\n\n## truncateJson(jsonString, maxSize)\n\n`jsonString` `string`\\\n`maxSize` `number`\\\n_Return value_: [`object`](#return-value)\n\nTruncates a JSON string to `maxSize` bytes.\n\nAny object property or array item beyond the `maxSize` limit is completely\nomitted. Strings are not truncated, except when at the top-level.\n\n### Return value\n\nThe return value is an object with the following properties.\n\n#### jsonString\n\n_Type_: `string`\n\n`jsonString` after truncation has been applied.\n\n#### truncatedProps\n\n_Type_: `object[]`\n\nList of properties having been truncated/omitted.\n\n##### truncatedProps[*].path\n\n_Type_: `Array\u003cstring | number\u003e`\n\nProperty path. This is an array of property keys and/or array indices.\n\n##### truncatedProps[*].value\n\n_Type_: `JsonValue`\n\nProperty value.\n\n# Related projects\n\n- [`is-json-value`](https://github.com/ehmicky/is-json-value): Check if a value\n  is valid JSON\n- [`safe-json-value`](https://github.com/ehmicky/safe-json-value): ⛑️ JSON\n  serialization should never fail\n- [`guess-json-indent`](https://github.com/ehmicky/guess-json-indent): Guess the\n  indentation of a JSON string\n- [`string-byte-length`](https://github.com/ehmicky/string-byte-length): Get the\n  UTF-8 byte length of a string\n- [`string-byte-slice`](https://github.com/ehmicky/string-byte-slice): Like\n  `string.slice()` but bytewise\n\n# Support\n\nFor any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).\n\nEveryone is welcome regardless of personal background. We enforce a\n[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and\ninclusive environment.\n\n# Contributing\n\nThis project was made with ❤️. The simplest way to give back is by starring and\nsharing it online.\n\nIf the documentation is unclear or has a typo, please click on the page's `Edit`\nbutton (pencil icon) and suggest a correction.\n\nIf you would like to help us fix a bug or add a new feature, please check our\n[guidelines](CONTRIBUTING.md). Pull requests are welcome!\n\n\u003c!-- Thanks go to our wonderful contributors: --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START --\u003e\n\u003c!-- prettier-ignore --\u003e\n\u003c!--\n\u003ctable\u003e\u003ctr\u003e\u003ctd align=\"center\"\u003e\u003ca href=\"https://fosstodon.org/@ehmicky\"\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/8136211?v=4\" width=\"100px;\" alt=\"ehmicky\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eehmicky\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/ehmicky/truncate-json/commits?author=ehmicky\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#design-ehmicky\" title=\"Design\"\u003e🎨\u003c/a\u003e \u003ca href=\"#ideas-ehmicky\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"https://github.com/ehmicky/truncate-json/commits?author=ehmicky\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\n --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehmicky%2Ftruncate-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fehmicky%2Ftruncate-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehmicky%2Ftruncate-json/lists"}