Skip to main content
Programming Language Tools

JSON to JavaScript

Convert JSON into a JavaScript object/array literal, ready to paste directly into JS code as a const declaration.

Paste JSON below - this tool wraps it as a JavaScript const declaration you can paste directly into your code.

Since JSON is already valid JavaScript object/array syntax, this mostly just adds a variable declaration - the real value is a ready-to-paste, named constant.

What Does This Tool Do?

It takes your JSON and wraps it as a JavaScript const variable declaration - since valid JSON is already valid JavaScript object/array literal syntax, the main value is producing a ready-to-paste, properly named constant.

Why Use This Tool?

Useful for quickly turning an API response or config JSON into a mock data file or test fixture constant in a JavaScript/TypeScript project.

How to Use It

  1. Paste valid JSON.
  2. Enter a variable name for the constant.
  3. Copy the generated const declaration into your JS/TS file.

Limitations

This generates a plain JavaScript value - for a typed version (a TypeScript interface describing the shape), see the JSON to TypeScript tool.

Example

JSON from an API:

{"id": 1042, "name": "Asha", "roles": ["editor"]}

with the variable name mockUser becomes:

const mockUser = {
  id: 1042,
  name: 'Asha',
  roles: [
    'editor'
  ]
};

Where It Helps

  • Test fixtures for Jest, Vitest or Cypress - paste a real response as a mock.
  • Storybook stories and component demos that need realistic data.
  • Prototypes that use hard-coded data before an API exists.

Tips

  • Add export in front to share the constant between files: export const mockUser = {...}.
  • Freeze fixtures so tests cannot change them by accident: Object.freeze(mockUser).
  • Remove real personal data - names, emails, tokens - before committing a copied API response.
  • For TypeScript, generate an interface too, so the fixture is type-checked.

Generate types with JSON to TypeScript. Need PHP or Python instead? See JSON to PHP Array and JSON to Python.

Frequently Asked Questions

JSON is a subset of JavaScript syntax. The converter produces a cleaner object literal with unquoted keys where possible, wrapped in a const declaration.

Object literals can be imported directly, allow comments and can be edited more easily inside source code.

Yes. Keys that are not valid JavaScript identifiers, such as "first-name", stay in quotes.
Share this tool: