# JSON to TSV string records v1

Save `json-tsv.mjs` beside `json-tsv-example.mjs` containing this example.
Run `node json-tsv-example.mjs` with Node 18+; no runtime dependencies or network.

```js
import assert from 'node:assert/strict';
import { jsonToTsv } from './json-tsv.mjs';
const result = jsonToTsv('[{"id":"001","note":""},{"id":"002"}]', 'note\tid', 'blank');
assert.equal(result.tsv, 'note\tid\n\t001\n\t002');
assert.equal(result.rows, 2);
assert.equal(result.columns, 2);
console.log('JSON to TSV example passed.');
```

## Contract

`jsonToTsv(input, headersInput, missingPolicy = 'reject')` returns
`{ tsv, report, rows, columns }`. Supply JSON containing an array of flat records
whose values are strings, plus one literal tab-separated header line. Headers
set the output column order. Literal header spaces remain; empty or
whitespace-only names and duplicate names reject. Extra record fields reject.
Missing fields reject by default; explicitly pass `'blank'` to emit empty cells.
An empty array produces the header alone, with no terminal newline.

Values preserve leading zeros, empty strings, spaces and formula text.
Numbers, booleans, null, nested objects and arrays reject. Tabs, LF and CR in
values reject because this format has no escaping or quoted multiline cells.
Header line endings reject as well. Special keys such as `__proto__`,
`constructor` and `toString` remain inert own properties. JSON.parse resolves
duplicate object keys using the last value; this helper cannot detect earlier
values after parsing.

Limits: 100,000 JSON input characters, 50 columns, 499 records, and 300,000
output TSV characters. Long headers can reach the output bound, including
the separators added for records.
Errors stop conversion. No file writes, requests or formula evaluation occur;
formula text may be interpreted by a receiving spreadsheet. Validate the
receiving application's import rules separately. Native export plus a local
script is an alternative; no measured advantage or user demand is claimed.

## Source parity

Generated from the browser's exact `site/lib/json-tsv.ts` core with
`node scripts/build-json-tsv.mjs`; use `--check` to verify bytes. Tests compare
runtime results/errors and execute this example. A download is not evidence
of independent local execution. Input and output are not sent in analytics.

## License

MIT License

Copyright (c) 2026 QuasiLife

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
