Paste JSON and get a JavaScript object literal. A key loses its quotes when it is a valid identifier and keeps them when it is not, so firstName comes out bare and first-name stays quoted. Use it to turn an API response into a mock, a fixture or a component's initial state.
[
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"roles": [
"admin",
"editor"
],
"active": true
},
{
"id": 2,
"name": "Jane Smith",
"email": "jane@example.com",
"roles": [
"user"
],
"active": false
}
] const users = [
{
id: 1,
name: 'John Doe',
email: 'john@example.com',
roles: [
'admin',
'editor'
],
active: true
},
{
id: 2,
name: 'Jane Smith',
email: 'jane@example.com',
roles: [
'user'
],
active: false
}
]; Paste a real response, copy the object literal and assign it in a test file. The test then runs without a network call.
The starting value for a React or Vue component, already in the shape the API returns.
Move a JSON config into a module when you need comments. A .js file can export the object, explain a setting and compute a value, and JSON allows none of those.
const util = require("node:util");
const raw = '{"id":1,"first-name":"Ada","tags":["a","b"],"meta":{"active":true}}';
const data = JSON.parse(raw);
const literal = util.inspect(data, {
depth: null,
compact: false,
maxArrayLength: null,
maxStringLength: null,
});
console.log("export default " + literal + ";"); util.inspect prints valid JavaScript for plain data. The maxArrayLength and maxStringLength options stop it truncating with "... more items".
import json, re
raw = '{"id":1,"first-name":"Ada","tags":["a","b"],"meta":{"active":true}}'
text = json.dumps(json.loads(raw), indent=2)
# Drop quotes around keys that are valid JS identifiers
literal = re.sub(r'^(\s*)"([A-Za-z_$][\w$]*)":', r'\1\2:', text, flags=re.M)
print("export default " + literal + ";") The regex only touches lines that start with a key, so string values containing a colon are left alone.
Every JSON document is a valid JavaScript expression, so this conversion is cosmetic: quotes come off the keys and the values stay the same. In the Node snippet, util.inspect also switches strings to single quotes. There was one historical exception, the U+2028 and U+2029 line separator characters, which JSON allowed inside strings and older JavaScript engines rejected. ES2019 fixed that.
Check whether you need this at all. Bundlers import .json files directly, and so does Node with the import attribute { type: "json" }. Convert to a .js module when you want comments in the data, computed values or a TypeScript type on the export.
This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.
JSON is a strict text format: keys must be double-quoted, and it allows no trailing commas, comments, functions or undefined. A JavaScript object literal is looser and lives in code. This tool rewrites the JSON as an object literal and removes the quotes from keys wherever the language allows it.
No. JSON cannot contain functions, so there is nothing to convert. The tool handles data only: objects, arrays, strings, numbers, booleans and null.
Yes. Object literal notation has been valid since the first version of JavaScript, so the output works in ES modules, CommonJS, Node.js and TypeScript.
A key that is not a valid identifier keeps its quotes, for example one that contains a space or a hyphen, or one that starts with a digit. Valid identifiers such as userId lose their quotes.
Use JSON.parse() for data that arrives at runtime. This tool is for source code: a default config object, mock data in tests, a fixtures file or a Storybook story, where a readable literal is easier to work with than a JSON string.
JSON to PHP array does the same job for backend code. CSV to JavaScript starts from a spreadsheet, and the JSON formatter shows where input that will not parse goes wrong.