JS data literal converter
All three directions are real conversions: JSON to a JS object literal (unquoted keys, single-quoted strings), JS object literal to JSON (a lenient hand-written parser that accepts single or double quotes, unquoted keys, trailing commas and comments) and JSON to TypeScript interfaces (array union literal types, nested objects become sub-interfaces). Unparsable input throws invalid-json / invalid-js-literal instead of guessing.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
Result
The result can be copied directly.
Paste JSON or a JS object literal to convert it
Characters: 0 · Lines: 0 · Direction: 0 · Keys: 0
What this tool does
- Turn API JSON into a JS literal you can paste into code.
- Generate a TypeScript interface skeleton from sample data.
- Clean up a commented, trailing-comma config literal into standard JSON.
Example
Input
{"id": 1, "name": "Ada", "tags": ["a", "b"], "meta": {"active": true}}Output
const data = {
id: 1,
name: 'Ada',
tags: [
'a',
'b'
],
meta: {
active: true
}
};js2json uses a hand-written parser and never executes code (no eval / Function), so only literals are supported — no variables or function calls.
Frequently asked questions
How does JS → JSON parsing work?
A hand-written recursive descent parser: single or double quoted strings, unquoted keys, trailing commas, `//` and block comments and unicode escapes. It only parses literals and never executes input code.
How are mixed arrays typed for TS?
As a union, e.g. [1, "a"] becomes (number | string)[]; nested objects become sub-interfaces such as Level1 / Level2, and the root type is noted at the end.
Why can undefined not be converted?
JSON has no undefined, so the conversion fails with invalid-js-literal rather than silently dropping fields. Replace it with null first.
Is key order preserved?
Yes. json2js and json2ts follow the input order, and js2json rebuilds objects in encounter order, so ordering is kept.
Keywords:json to ts