json-to-js
A JSON-to-JS converter transforms strict JSON into a JavaScript object literal β unquoted keys (where valid identifier), single quotes (more idiomatic JS), optional const x = \{...\} wrapper, and configurable indentation. Useful for: pasting JSON config into a JS file with cleaner syntax, generating mock data fixtures from JSON API responses, and producing easier-to-read JS source from JSON. The ZTools JSON to JS Converter handles unicode keys properly (only valid JS identifiers go unquoted), optionally promotes ISO date strings to new Date(...) calls, and emits ESLint-friendly output.
Use casesβ
JSON config β JS moduleβ
A .json config promoted to .js module to allow comments and computed values: tool generates export const config = \{...\} from JSON.
API response β mock fixtureβ
Captured a JSON API response; want JS-format fixture for tests. Cleaner syntax + comments support after conversion.
JSON-blob doc β readable JSβ
A 200-line JSON config is hard to read. JS object literal with unquoted keys + single quotes is friendlier.
Migration: JSON β CommonJS moduleβ
module.exports = require("./config.json") β just import-as-JS for a richer module.
How it worksβ
- Paste JSON β Strict valid JSON.
- Pick output style β Single quotes (default JS idiom) or double; unquote valid-identifier keys; wrap in
const x = \{...\}. - Date promotion (optional) β Detected ISO 8601 strings (
"2026-05-06T...") becomenew Date("2026-05-06T...")calls. - Convert β AST-driven; output is ESLint-friendly.
- Copy / download β .js file or clipboard.
Examplesβ
Input: \{"name":"Alice","age":30,"active":true\}
Output: \{ name: 'Alice', age: 30, active: true \} β keys unquoted, single quotes, idiomatic JS.
Input: \{"created":"2026-05-06T10:00:00Z"\}
Output: \{ created: new Date('2026-05-06T10:00:00Z') \} β ISO promoted to Date.
Input: JSON with "2nd-key": ...
Output: "2nd-key" stays quoted (not a valid JS identifier).
Frequently asked questionsβ
When does a key stay quoted?
When it's not a valid JS identifier: starts with digit, contains hyphens/spaces, is a reserved word, or is non-ASCII. Tool quotes those automatically.
Single vs double quotes?
Both valid JS. Single quotes are more idiomatic in modern JS / TS codebases (+ standard ESLint rule). Tool defaults to single; configurable.
Will output be ESLint-clean?
Generally yes β unquoted-when-possible keys + single quotes + 2-space indent matches default ESLint config.
Can the JS be parsed back?
Yes β output is valid JS and eval or (...)-wrap evaluates to the same object. For round-trip, use the JS-to-JSON converter.
Date promotion safe?
Mostly β but ISO format detection is heuristic. Strings that look like dates but aren't may be wrongly promoted. Disable for safety in those cases.
Comments on output?
Optional toggle: insert "// generated from JSON" header. Useful for tracking provenance.
Tipsβ
- Single quotes + unquoted keys + 2-space indent = idiomatic modern JS.
- Use date promotion only when you need actual Date objects; otherwise strings are simpler.
- For TypeScript output, pair this tool with the JS-to-TS converter β gets you typed JS from JSON.
- For very large JSON (10k+ lines), consider keeping JSON-as-is and
import json from "./data.json". - For dynamically-keyed objects, output may have many quoted keys β that's normal, JS rules.
Try it nowβ
The full json-to-js runs in your browser at https://ztools.zaions.com/json-to-js β no signup, no upload, no data leaves your device.
Last updated: 2026-05-06 Β· Author: Ahsan Mahmood Β· Edit this page on GitHub