{"id":16691341,"url":"https://github.com/dphilipson/untruncate-json","last_synced_at":"2025-03-17T00:33:20.653Z","repository":{"id":47378666,"uuid":"220723406","full_name":"dphilipson/untruncate-json","owner":"dphilipson","description":"Fix up the end of a partial JSON string to create valid JSON.","archived":false,"fork":false,"pushed_at":"2023-01-05T00:44:31.000Z","size":1200,"stargazers_count":33,"open_issues_count":15,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-14T10:52:23.724Z","etag":null,"topics":["json","json-string","validation"],"latest_commit_sha":null,"homepage":null,"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/dphilipson.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}},"created_at":"2019-11-10T00:54:07.000Z","updated_at":"2024-04-13T20:57:39.000Z","dependencies_parsed_at":"2023-02-03T02:01:05.552Z","dependency_job_id":null,"html_url":"https://github.com/dphilipson/untruncate-json","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dphilipson%2Funtruncate-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dphilipson%2Funtruncate-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dphilipson%2Funtruncate-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dphilipson%2Funtruncate-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dphilipson","download_url":"https://codeload.github.com/dphilipson/untruncate-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221669483,"owners_count":16860892,"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":["json","json-string","validation"],"created_at":"2024-10-12T16:07:57.146Z","updated_at":"2024-10-27T11:36:39.689Z","avatar_url":"https://github.com/dphilipson.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# untruncate-json\n\nFix up the end of a partial JSON string to create valid JSON.\n\n[![Build Status](https://travis-ci.com/dphilipson/untruncate-json.svg?branch=master)](https://travis-ci.com/dphilipson/untruncate-json)\n\n## Motivation\n\nHave you ever been given a string that started off as valid JSON, but was then\ntruncated to fit under some maximum length? And even though it was invalid JSON\nbecause it was cut off in the middle, you still wanted to parse it as JSON\nanyways so you could extract what information you could from it?\n\nNo?\n\nWell if you ever do, then this is the library for you!\n\n## Installation\n\nWith NPM:\n\n```\nnpm install untruncate-json\n```\n\nWith Yarn:\n\n```\nyarn add untruncate-json\n```\n\n## Usage\n\nImport the `untruncateJson` function:\n\n```ts\nimport untruncateJson from \"untruncate-json\";\n```\n\nCall it on a truncated JSON string to get a complete, valid JSON string:\n\n```ts\nuntruncateJson(\"[1, 2\"); // -\u003e \"[1, 2]\"\nuntruncateJson('\"Hello, Wor'); // -\u003e '\"Hello, Wor\"'\nuntruncateJson('{\"votes\": [true, fa'); // -\u003e '{\"votes\": [true, false]}'\nuntruncateJson(\"123.\"); // -\u003e \"123.0\"\n```\n\n`untruncateJson` will sometimes remove characters as well, if there was no way\nto infer what value they were starting:\n\n```ts\nuntruncateJson(\"[1, 2, \"); // -\u003e \"[1, 2]\"\nuntruncateJson('\"abc\\\\'); // -\u003e '\"abc\"'\nuntruncateJson('{\"x\": 20, \"y\": '); // -\u003e '{\"x\": 20}'\n```\n\nCheck the [test\ncases](https://github.com/dphilipson/untruncate-json/blob/master/test/index.test.ts)\nfor many more examples.\n\n## Guarantees\n\n- As long as the input is the prefix of a valid JSON string and is not entirely\n  whitespace, the output will be valid JSON.\n- This takes into account all parts of the JSON spec, including rules around\n  exponents in numbers, Unicode escape sequences in strings, and so forth.\n- **`untruncateJson` does not attempt to validate the input JSON.** Therefore if\n  the input JSON is not the prefix of valid JSON, then the output will not be\n  valid JSON and will fail if you try to parse it.\n- The output will always contain as many correct values as can be determined\n  under the assumption that the input was indeed the prefix of valid JSON. For\n  example, if the input is cut off after a bare `t`, then `untruncateJson` will\n  assume that it was the start of the token `true`.\n- The last value appearing in the output JSON may have an incorrect value, for\n  example if it is a truncated number or string. However, it will never have the\n  incorrect _type_, e.g. if the input contained a string in that position, so\n  will the output.\n- In cases where the type of the last value cannot be inferred, then characters\n  are removed from the end of the input as needed to avoid making a potentially\n  incorrect guess. For example, if the truncation occurs after an object key but\n  before any part of its corresponding value, then the entire key will be\n  removed.\n\nCopyright © 2019 David Philipson\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdphilipson%2Funtruncate-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdphilipson%2Funtruncate-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdphilipson%2Funtruncate-json/lists"}