{"id":13494077,"url":"https://github.com/wooorm/plain-text-data-to-json","last_synced_at":"2025-04-19T11:55:54.601Z","repository":{"id":20273442,"uuid":"23546572","full_name":"wooorm/plain-text-data-to-json","owner":"wooorm","description":"Transform a simple plain-text database to JSON","archived":false,"fork":false,"pushed_at":"2022-11-14T08:22:38.000Z","size":92,"stargazers_count":14,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-12T19:51:36.647Z","etag":null,"topics":["data","json","plain","text","txt"],"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/wooorm.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":"funding.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"wooorm"}},"created_at":"2014-09-01T15:13:04.000Z","updated_at":"2025-01-02T11:08:45.000Z","dependencies_parsed_at":"2023-01-13T20:52:49.421Z","dependency_job_id":null,"html_url":"https://github.com/wooorm/plain-text-data-to-json","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Fplain-text-data-to-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Fplain-text-data-to-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Fplain-text-data-to-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Fplain-text-data-to-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wooorm","download_url":"https://codeload.github.com/wooorm/plain-text-data-to-json/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249690019,"owners_count":21311307,"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":["data","json","plain","text","txt"],"created_at":"2024-07-31T19:01:21.551Z","updated_at":"2025-04-19T11:55:54.567Z","avatar_url":"https://github.com/wooorm.png","language":"JavaScript","funding_links":["https://github.com/sponsors/wooorm"],"categories":["JavaScript"],"sub_categories":[],"readme":"# plain-text-data-to-json\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n\nTransform basic plain-text lists or objects into arrays and objects.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`toJson(value[, options])`](#tojsonvalue-options)\n*   [Data](#data)\n    *   [Comments](#comments)\n    *   [Whitespace](#whitespace)\n    *   [Empty lines](#empty-lines)\n    *   [Key/value pairs](#keyvalue-pairs)\n    *   [Values](#values)\n    *   [Errors](#errors)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Contribute](#contribute)\n*   [Security](#security)\n*   [License](#license)\n\n## What is this?\n\nThis package takes a file (a sort of simple database), parses it, and returns\nclean data.\n\n## When should I use this?\n\nI found myself rewriting a simple transformation over and over to handle text\nfiles.\nOne example is this source file in [`emoji-emotion`][emoji-emotion-example]\nThis project fixes that for me.\nYou can use it too if it matches your needs.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 14.14+, 16.0+), install with [npm][]:\n\n```sh\nnpm install plain-text-data-to-json\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {toJson} from 'https://esm.sh/plain-text-data-to-json@2'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {toJson} from 'https://esm.sh/plain-text-data-to-json@2?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nIf we have the following file `input.txt`:\n\n```txt\n% A comment\n\nalpha\nbravo\ncharlie\n```\n\n…and our module `example.js` looks as follows:\n\n```js\nimport fs from 'node:fs/promises'\nimport {toJson} from 'plain-text-data-to-json'\n\nconst document = String(await fs.readFile('input.txt'))\n\nconst data = toJson(document)\n\nawait fs.writeFile('output.json', JSON.stringify(data, null, 2) + '\\n')\n```\n\n…then running `node example.js` yields in `output.json`:\n\n```json\n[\n  \"alpha\",\n  \"bravo\",\n  \"charlie\"\n]\n```\n\n## API\n\nThis package exports the identifier `toJson`.\nThere is no default export.\n\n### `toJson(value[, options])`\n\nTransform basic plain-text lists or objects into arrays and objects.\n\n##### `options`\n\nConfiguration (optional).\n\n###### `options.delimiter`\n\nCharacter to use as delimiter between key/value pairs (`string`, default:\n`':'`).\n\n###### `options.comment`\n\nCharacter(s) to use for line comments, `false` turns off comments (`string`,\n`Array\u003cstring\u003e`, or `boolean`, default: `'%'`)\n\n###### `options.forgiving`\n\nHow relaxed to be (`'fix'` or `boolean`, default: `false`).\nWhen `true`, doesn’t throw for duplicate keys.\nWhen `'fix'`, doesn’t throw for key/value pairs and overwrites (see\n[errors][]).\n\n###### `options.log`\n\nWhether to call `console.log` with info when `forgiving` ignores an error\n(`boolean`, default: `true`).\n\n## Data\n\nThe term plain text might be confusing.\nIt’s actually more of some (sparingly specified) standard.\n\n### Comments\n\nUse a percentage sign (by default) to specify a comment.\nThe comment will last until the end of line.\n\n```txt\n% This is a completely commented line.\nunicorn % This is a partially commented line.\n```\n\nYields:\n\n```js\n['unicorn']\n```\n\n### Whitespace\n\nInitial or final white space (`\\s`) is trimmed from values.\n\n```txt\n       unicorn     % some value\n```\n\nYields:\n\n```js\n['unicorn']\n```\n\n### Empty lines\n\nEmpty lines are striped.\nThis includes whitespace only lines.\n\n```txt\n    %%% this file contains a value. %%%\n\nunicorn\n```\n\nYields:\n\n```js\n['unicorn']\n```\n\n### Key/value pairs\n\nIf a line includes a colon (by default), the library returns an object.\n\n```txt\nunicorn : magic creature\n```\n\nYields:\n\n```js\n{unicorn: 'magic creature'}\n```\n\n### Values\n\nAll other lines are treated as array values.\n\n```txt\nunicorn\n```\n\nYields:\n\n```json\n[\"unicorn\"]\n```\n\n### Errors\n\nSome errors are thrown when malformed “plain-text” is found, such as:\n\n*   when lines both with and without colons exist\n*   in arrays, when duplicate values exist (unless `forgiving: true`)\n*   in objects, when duplicate properties exist (unless `forgiving: true`)\n*   in objects, when duplicate properties with different values exist (unless\n    `forgiving: \"fix\"`)\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type `Options`.\n\n## Compatibility\n\nThis package is at least compatible with all maintained versions of Node.js.\nAs of now, that is Node.js 14.14+ and 16.0+.\nIt also works in Deno and modern browsers.\n\n## Contribute\n\nYes please!\nSee [How to Contribute to Open Source][contribute].\n\n## Security\n\nThis package is safe.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/wooorm/plain-text-data-to-json/workflows/main/badge.svg\n\n[build]: https://github.com/wooorm/plain-text-data-to-json/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/plain-text-data-to-json.svg\n\n[coverage]: https://codecov.io/github/wooorm/plain-text-data-to-json\n\n[downloads-badge]: https://img.shields.io/npm/dm/plain-text-data-to-json.svg\n\n[downloads]: https://www.npmjs.com/package/plain-text-data-to-json\n\n[size-badge]: https://img.shields.io/bundlephobia/minzip/plain-text-data-to-json.svg\n\n[size]: https://bundlephobia.com/result?p=plain-text-data-to-json\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[contribute]: https://opensource.guide/how-to-contribute/\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[errors]: #errors\n\n[emoji-emotion-example]: https://github.com/words/emoji-emotion/blob/main/faces.txt\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwooorm%2Fplain-text-data-to-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwooorm%2Fplain-text-data-to-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwooorm%2Fplain-text-data-to-json/lists"}