# JSON field rename v1

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

```js
import assert from 'node:assert/strict';
import { renameJsonField } from './json-rename.mjs';
const input = JSON.stringify([
  { id: '001', note: 'line\nnext', status: '' },
  { id: '002', note: '', status: 'ready' },
]);
const result = renameJsonField(input, 'note', 'comment');
assert.equal(result.records, 2);
assert.equal(result.fields, 3);
assert.deepEqual(JSON.parse(result.output), [
  { id: '001', comment: 'line\nnext', status: '' },
  { id: '002', comment: '', status: 'ready' },
]);
assert.equal(result.output, '[{"id":"001","comment":"line\\nnext","status":""},{"id":"002","comment":"","status":"ready"}]');
console.log('JSON field rename example passed.');
```

## Contract

`renameJsonField(input, oldName, newName)` returns `{ output, records, fields }`.
`fields` counts fields in the first record; other records may have different
fields. Supply a nonempty JSON array of flat objects with string values only.
Supply explicit nonempty old and new field names as exact strings. Names are
never trimmed or normalized. Whitespace-only names and embedded controls are
literal names. Every record must contain the exact old name as an own property.
A distinct new name must be absent as an own property from every record; a
collision rejects the whole operation even when the values match. Identical
old and new names are allowed, with full record and own-field validation.

The entire input is validated, including fields outside the rename. Invalid
JSON, non-array roots, empty arrays, non-object records and numbers, booleans,
null, arrays or nested objects reject. Failure produces no partial output.
Errors never echo names or values. Output is compact JSON with no terminal
newline. The input string is unchanged.

Exact values preserve leading zeros, empty strings, spaces, formula text,
Unicode and escaped tabs, CR and newlines. Formulas are not evaluated. Special
names such as `__proto__`, `constructor` and `toString` remain inert own
properties. The renamed key retains its position among non-index keys;
integer index-like keys follow standard JavaScript property ordering instead.
JSON.parse resolves duplicate input keys using the last value. Validation
applies after that resolution; earlier duplicate values cannot be recovered.

Limits: 100,000 input characters, 100,000 characters per field name, 499 records,
50 fields per record and 300,000 output characters, measured as JavaScript
string length. Longer names repeated across records and escaped lone Unicode
surrogates count toward the output limit. The helper performs no requests,
file writes, type coercion or formula evaluation. 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-rename.ts` core with
`node scripts/build-json-rename-source.mjs`; use `--check` to verify bytes.
Tests compare runtime results/errors at input/output bounds and execute this
example. A download is not evidence of independent local execution. Input,
output and field names 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.
