Skip to content

JSON to JavaScript Array

Wrap JSON in a JavaScript array literal.

Input
Output

No result

About JSON to JavaScript Array

JSON to JavaScript Array wraps a JSON value in a JavaScript declaration so it can be dropped straight into a script or a module. Paste a JSON array and the page returns the same data indented with two spaces, preceded by the words const data = and followed by a semicolon. It is a small formatting step for the very common job of moving data out of a JSON file and into code.

The conversion is a parse followed by a re-serialisation, which means the input is validated on the way through. A malformed document raises a parse error rather than producing output with a piece missing, so a result appearing on the page also confirms that the JSON was well formed to begin with.

Two limitations follow from how narrow the step is. The variable name is fixed and there is no field to change it, so the declaration has to be renamed by hand if data is already in use where you paste it. And the output is a whole statement rather than a bare array, so if you wanted only the bracketed value to pass as an argument, the prefix and the trailing semicolon have to be removed. The name also overpromises a little: nothing checks that the input really is an array, so an object or a single number is wrapped just as happily.

How to use JSON to JavaScript Array

  1. 1 Paste your JSON into the input box, or keep the sample array already there.
  2. 2 The declaration is assembled as you type, so there is no button to press.
  3. 3 Copy the result into your script or module.
  4. 4 Rename the variable if data is already in use where you paste it.

Frequently asked questions

Can I change the variable name?

No. The declaration always uses the name data and there is no control to override it. Rename it after pasting if that name is already taken in the file you are working in.

Does the input have to be an array?

No, despite the tool name. Any valid JSON document is accepted and wrapped as-is, so a single object, a string or a number produces a declaration that parses correctly but looks nothing like an array.

What happens if my JSON is invalid?

The conversion raises a parse error and no output is produced, because the input is parsed before it is written back out. A result appearing on the page therefore also confirms the input parsed cleanly.

How is the output indented?

With two spaces at every level of nesting, the indentation used by most JavaScript style guides, and the declaration is closed with a semicolon.

Free, online, and private

Free to use

JSON to JavaScript Array is completely free. There is no account to create, no login, and no hidden charge or usage limit.

Runs online in your browser

It is a free online tool that runs entirely in your browser. Nothing to download or install — open the page and start using it right away.

Your data stays private

All processing happens locally on your device. Your input is never uploaded, stored, or tracked, so it never leaves your browser.

How JSON to JavaScript Array behaves in detail

These are the specifics of this implementation — the exact ranges, defaults and fixed settings you are working with.

Parameter Value
Input a JSON document, parsed with the standard JSON parser
Prefix the literal text "const data = " is prepended to the value
Indentation two spaces, produced by passing an indent argument to the serialiser
Terminator a semicolon is appended after the closing bracket
Invalid input a parse error is raised and nothing partial is emitted
Update mode the result refreshes as you type; there is no run button

Compiled from the implementation of this tool and checked against it by itoolnet.

What JSON to JavaScript Array does not do

Knowing the edges of a tool saves more time than another list of its features. These are the cases where it will not do what you might expect.

  • The variable name is fixed to data. There is no field to rename it, so the declaration has to be edited by hand or it will collide with an existing variable of the same name in the file it is pasted into, and nothing on the page indicates that the name was chosen for you rather than detected from the input.
  • The output is a whole statement, not a bare array literal. It reads as a variable declaration followed by the value and a semicolon, so if what you wanted was just the bracketed value to drop into a function argument, a configuration file or a JSON field, the prefix and the trailing semicolon have to be stripped off first.
  • Despite the name, the input does not have to be an array. Any valid JSON document is accepted and wrapped as-is, so a single object, a string or a number produces a declaration that parses perfectly well yet looks nothing like the array the tool name promised, and no shape check warns you.

Tested and documented by itoolnet, the team that builds and maintains this site.

Share & embed this tool

Writing a guide or article? You are welcome to link to JSON to JavaScript Array with the snippet below. It is free, runs entirely in your browser, and needs no signup.