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.
More Programming Language Tools
View all 23Since 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
- Paste valid JSON.
- Enter a variable name for the constant.
- Copy the generated
constdeclaration 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
exportin 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
Explore More Tools
Keep going with related categories and our most used tools.