Back to Tools

JSON to JS Object

Convert JSON strings into native JavaScript Object format

JavaScript Tool
Source JSON
JS Object Format

                

Paste JSON, get a declaration ready for your source file

API responses and config files arrive as JSON, but when you want that data inside a script as a constant, quoted keys everywhere make it noisy. This tool parses your JSON, strips the quotes from property names, and wraps the result in a variable declaration so you can drop it directly into a JavaScript file.

Steps

  1. Paste your JSON into Source JSON, or press Load Sample to see a user and permissions example.
  2. Type a name in Variable Name. It defaults to data, and blank falls back to data as well.
  3. Pick a Variable Type: const, let, var, or None (Object only) for the bare literal.
  4. Click Convert to JS Object and copy the text from the JS Object Format panel.

If the input is not valid JSON, an alert says Invalid JSON and includes the parser's message so you can locate the problem.

What the output looks like

The JSON is parsed, re-indented with four spaces, keys are unquoted, and the result is run through a JavaScript beautifier. Strings keep their double quotes. A declaration such as const data = { user: "admin", active: true }; is the typical result, laid out over several lines.

Where it is useful

  • Embedding an API sample response as a fixture in a unit test.
  • Pasting settings from a JSON file into an inline script or a configuration module.
  • Building mock data for a front-end prototype without a backend.
  • Rewriting a JSON snippet in documentation to match the style of your codebase.

Limits to watch for

Unquoting is done with a pattern replacement, and it applies to every key. That is fine for plain names, but a key that is not a valid identifier, such as one containing a hyphen or space, or starting with a digit, will come out unquoted and break the resulting code. If your keys look like first-name or 2024, quote them again by hand. A pattern like that can also affect text inside a string value that resembles a key followed by a colon. Review the result before committing it. The conversion runs in your browser and does not need a server round trip.

Related Tools


Frequently Asked Questions

Are the property names always unquoted?
Yes, the tool removes quotes from every key. That works for simple names like user or active, but keys with hyphens, spaces or a leading digit will not be valid unquoted in JavaScript, so re-add quotes to those manually.
What does None (Object only) do?
It outputs just the object or array literal, with no const, let or var and no variable name. This is handy when you want to pass the value directly into a function call or assign it yourself.
Why do I get an Invalid JSON alert?
The input failed strict JSON parsing. Common causes are single-quoted strings, trailing commas, comments or unquoted keys. The alert includes the parser's message, which usually points to the position of the problem.
Will string values keep their double quotes?
Yes. Only key names are unquoted. Values stay as they were in the JSON, with double quotes around strings. If your style guide prefers single quotes, run the result through the JavaScript Formatter's quote option.
Can I convert a JSON array instead of an object?
Yes. Any valid JSON value works, including arrays of objects, nested structures and plain values. The output is the equivalent JavaScript literal, optionally assigned to your chosen variable name using const, let or var.
Support Chat
We typically reply within minutes

Hello! 👋 Before we start, please tell us your name.