JSON to JavaScript

Convert JSON into a ready to paste JavaScript object literal, or into JSDoc typedefs that describe its shape.

JSON
JavaScript
Runs locally in your browser

About the JSON to JavaScript

Pasting raw JSON into a source file works, but it never matches the surrounding code: every key is quoted, strings use double quotes, and there is no declaration. This converter writes the same data as idiomatic JavaScript. Keys that are valid identifiers lose their quotes, keys that are not, such as font-stack or one that starts with a digit, keep them so the literal still parses. Strings are re-quoted in the style you choose and escaped correctly for it, including apostrophes inside single quoted strings.

The declaration selector wraps the value the way your file needs it: a plain const, a named export, a default export, a CommonJS assignment, or nothing at all when you are pasting into an argument list. The variable name is sanitised to a legal identifier, so a stray space or hyphen will not produce broken code.

Switch the output to JSDoc typedefs and the tool describes the structure instead of the data. Every distinct object shape becomes an @typedef block with one @property per key, arrays are written with the element type followed by brackets, and repeated shapes are reused rather than duplicated. That gives an editor enough information for autocomplete in a plain JavaScript project with no build step.

For the reverse direction use JavaScript to JSON, and if the project is typed, JSON to TypeScript emits interfaces instead of comments.

How to use

  1. Paste JSON on the left, or click Sample for a theme configuration example.
  2. Choose a Declaration and type the Variable name you want in your file.
  3. Pick the Quotes style that matches your linter, and untick Semicolon if your project omits them.
  4. Switch Output to JSDoc typedefs when you want the shape rather than the values.
  5. Copy the result or download it as data.js.

Common questions

Why do some keys keep their quotes?
Only keys that are valid JavaScript identifiers can be unquoted. Anything with a hyphen, a space, a leading digit or a reserved character keeps its quotes so the literal stays valid.
Is the generated object identical to the JSON?
Yes, the values are the same. Only the syntax around them changes, so parsing the JSON and evaluating the literal give equal objects.
What do the JSDoc typedefs give me?
Editors read them for autocomplete and type hints in plain JavaScript files, and they document the payload for the next person without adding a compile step.
Can I get a nested object as a separate named constant?
Not automatically. Convert the nested part on its own with a different variable name, then reference it from the parent literal.