JSON to JS Object

Converts JSON into a JavaScript object literal — unquoting keys that are valid identifiers, and switching to single quotes. Useful when pasting API data into source code as a fixture or a default.

Live demo · real result from this tool
Quotes

JavaScript object

const user = {  id: 1024,  name: 'Asha Rao',  'content-type': 'application/json',  tags: ['admin', 'editor'],  address: {    city: 'Pune',  },};

Processed on your device — nothing is uploaded.

How to use JSON to JS Object

  1. Paste your JSON.
  2. Copy the JavaScript object literal.
  3. Check any very large integers, which lose precision as JSON numbers.
  4. Keys that are not valid identifiers keep their quotes — that is correct, not a limitation.
  5. Integers beyond 2⁵³ lose precision; keep them as strings.
  6. Conversion runs in your browser; API data is not transmitted.

What actually differs between the two

JSON is a strict subset of JavaScript object syntax with the corners removed. JavaScript allows unquoted keys where they are valid identifiers, single quotes, trailing commas, comments, template literals, functions as values, undefined, NaN, Infinity and comment blocks. JSON allows none of those — double-quoted keys and strings, no trailing comma, no comments, null and nothing else. Converting JSON to JS is therefore always safe; converting JS to JSON is not.

Keys that cannot be unquoted

A key only loses its quotes if it is a valid JavaScript identifier: letters, digits, underscore and dollar, not starting with a digit. So content-type, user name and 2fa keep their quotes and are accessed with bracket notation. Reserved words are fine as property names in modern JavaScript, so class and default can be unquoted, though quoting them anyway reads more clearly.

Numbers are where a round trip loses data

JSON numbers become IEEE-754 doubles when parsed, so an integer beyond 2⁵³ loses precision — a 19-digit id like a Twitter snowflake or a database bigint comes back with its last digits changed, silently. If your data contains such values, keep them as strings or use BigInt literals in the JavaScript, which JSON cannot represent at all. This is the one conversion hazard worth checking for before pasting an API response into code.

Frequently asked questions

Why convert JSON to JavaScript?
An object literal reads more naturally in code: keys that are valid identifiers lose their quotes, strings can use single quotes, and trailing commas keep later diffs to one line. Give a variable name and the result is wrapped in a const declaration, ready to paste.
Is the output valid JavaScript?
Yes, the output is valid JavaScript object literal syntax that can be used directly in your code or assigned to variables.
Is JSON valid JavaScript?
Essentially yes. JSON is a subset of JavaScript object literal syntax, with two historical edge cases around unescaped line separators that were fixed in ES2019. The reverse does not hold: JavaScript allows unquoted keys, single quotes, trailing commas and comments, which JSON forbids. JSON is a data format; an object literal is code.
Why do some of my keys still have quotes?
Because they are not valid JavaScript identifiers. Anything containing a hyphen or space, or starting with a digit, must stay quoted and is accessed with bracket notation rather than a dot.
Can I convert JavaScript back to JSON?
Only if it contains nothing JSON lacks. Functions, undefined, NaN, Infinity, comments, trailing commas and BigInt values have no JSON equivalent, so a JavaScript object using any of them cannot round-trip.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close