{"id":51133682,"url":"https://github.com/trananhtung/jsonpluck","last_synced_at":"2026-06-25T15:01:48.601Z","repository":{"id":366610572,"uuid":"1275806436","full_name":"trananhtung/jsonpluck","owner":"trananhtung","description":"Pluck valid JSON out of messy LLM output — strips markdown fences, fixes trailing commas, single quotes, comments, and truncation. Zero dependencies.","archived":false,"fork":false,"pushed_at":"2026-06-22T14:55:11.000Z","size":56,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-22T16:16:04.644Z","etag":null,"topics":["ai","anthropic","json","llm","openai","parser","repair","structured-output","typescript","zero-dependency"],"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/trananhtung.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-21T06:44:09.000Z","updated_at":"2026-06-22T14:54:04.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/trananhtung/jsonpluck","commit_stats":null,"previous_names":["trananhtung/jsonpluck"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/trananhtung/jsonpluck","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fjsonpluck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fjsonpluck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fjsonpluck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fjsonpluck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trananhtung","download_url":"https://codeload.github.com/trananhtung/jsonpluck/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fjsonpluck/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34780126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-25T02:00:05.521Z","response_time":101,"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":["ai","anthropic","json","llm","openai","parser","repair","structured-output","typescript","zero-dependency"],"created_at":"2026-06-25T15:01:47.676Z","updated_at":"2026-06-25T15:01:48.588Z","avatar_url":"https://github.com/trananhtung.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonpluck\n\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)\n\n\u003e Pluck valid JSON out of messy LLM output — **markdown fences, trailing commas, single quotes, comments, truncation** — with **zero dependencies**.\n\n[![CI](https://github.com/trananhtung/jsonpluck/actions/workflows/ci.yml/badge.svg)](https://github.com/trananhtung/jsonpluck/actions/workflows/ci.yml)\n[![npm version](https://img.shields.io/npm/v/jsonpluck.svg)](https://www.npmjs.com/package/jsonpluck)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/jsonpluck)](https://bundlephobia.com/package/jsonpluck)\n[![types](https://img.shields.io/npm/types/jsonpluck.svg)](https://www.npmjs.com/package/jsonpluck)\n[![license](https://img.shields.io/npm/l/jsonpluck.svg)](./LICENSE)\n\nYou asked the model for JSON. You got:\n\n````text\nSure! Here's the data you requested:\n\n```json\n{\n  \"title\": \"Quarterly Report\",\n  \"tags\": [\"finance\", \"q3\",],   // trailing comma + comment\n  'confidence': 0.87,           // single quotes\n}\n```\n\nHope that helps!\n````\n\n`JSON.parse` throws on every line of that. **`jsonpluck` returns the object.**\n\n```ts\nimport { pluck } from \"jsonpluck\";\n\npluck(llmResponse);\n// → { title: \"Quarterly Report\", tags: [\"finance\", \"q3\"], confidence: 0.87 }\n```\n\n## What it handles\n\n- **Markdown fences** — ` ```json `, ` ```jsonc `, or plain ` ``` `.\n- **Prose around the JSON** — finds the first balanced `{…}` / `[…]`.\n- **Trailing commas**, **single quotes**, **“smart” quotes**, **unquoted keys**.\n- **`//` and `/* */` comments**.\n- **Python literals** — `True` / `False` / `None`.\n- **`NaN` / `Infinity`** → `null` (valid JSON).\n- **Truncated output** — a response cut off mid-object/array/string is recovered\n  as the partial value, with a `truncated` flag so you can decide what to do.\n\nStrictly-valid JSON takes a `JSON.parse` fast path, so there's no penalty for the\nhappy case.\n\n## Install\n\n```bash\nnpm install jsonpluck\n# or: pnpm add jsonpluck  /  yarn add jsonpluck  /  bun add jsonpluck\n```\n\n## API\n\n### `pluck\u003cT\u003e(text, options?): T`\n\nExtract and parse JSON, throwing if nothing is recoverable.\n\n```ts\nconst data = pluck\u003c{ title: string }\u003e(llmResponse);\n```\n\n### `tryPluck\u003cT\u003e(text, options?): PluckResult\u003cT\u003e`\n\nNever throws. Returns a discriminated union you can branch on.\n\n```ts\nconst r = tryPluck\u003cMySchema\u003e(llmResponse);\nif (r.ok) {\n  use(r.value);\n  if (r.truncated) console.warn(\"recovered a partial value\");\n} else {\n  console.error(r.error);\n}\n```\n\n```ts\ntype PluckResult\u003cT\u003e =\n  | { ok: true;  value: T; strict: boolean; truncated: boolean }\n  | { ok: false; error: string };\n```\n\n### `repair(text, indent?): string`\n\nReturn canonical JSON text (handy for logging or re-serialising).\n\n```ts\nrepair(\"{'a':1,}\");      // '{\"a\":1}'\nrepair(\"{'a':1}\", 2);    // '{\\n  \"a\": 1\\n}'\n```\n\n### Options\n\n| Option           | Type      | Default | Description                                            |\n| ---------------- | --------- | ------- | ------------------------------------------------------ |\n| `allowTruncated` | `boolean` | `true`  | Accept partial values from truncated input vs. fail.   |\n\n### Low-level building blocks\n\n```ts\nimport { extractCandidate, parseTolerant } from \"jsonpluck\";\n\nextractCandidate(text); // → the JSON-ish substring (or null)\nparseTolerant(jsonish); // → { value, strict, truncated }\n```\n\n## Why use it?\n\nLLMs are probabilistic; structured-output modes help but don't eliminate stray\nprose, fences, or truncation under token limits. Wrapping every parse in\n`jsonpluck` turns a class of intermittent production crashes into recovered data.\n\n- **Zero dependencies** — runs in Node, browsers, edge runtimes, and Workers.\n- **Fast path** for valid JSON — no overhead when the model behaves.\n- **Honest about truncation** — you get a flag, not a silent guess.\n\n## CLI\n\n```bash\ncat llm-response.txt | jsonpluck            # repaired JSON to stdout\ncat llm-response.txt | jsonpluck --pretty   # pretty-printed\njsonpluck response.json --strict-truncation # exit 1 if truncated\n```\n\n## A note on guarantees\n\n`jsonpluck` recovers *structure*; it cannot invent data that the model never\nproduced. For a truncated array of 10 items where only 6 arrived, you get 6.\nValidate the recovered value against your schema (e.g. with Zod) before trusting\nit.\n\n## Contributors ✨\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.\n\nThanks goes to these wonderful people:\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/trananhtung\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/30992229?v=4?s=100\" width=\"100px;\" alt=\"Tung Tran\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eTung Tran\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/trananhtung/jsonpluck/commits?author=trananhtung\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#maintenance-trananhtung\" title=\"Maintenance\"\u003e🚧\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\n## License\n\n[MIT](./LICENSE) © Tung Tran\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrananhtung%2Fjsonpluck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrananhtung%2Fjsonpluck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrananhtung%2Fjsonpluck/lists"}