Write Valid JSON Arrays

What a JSON array is, the syntax rules that make one valid, how it differs from a programming-language array, and how to keep it intact when copying or sharing.

A JSON array is an ordered list of values written as text, enclosed in square brackets, with values separated by commas. It is one of the two structured types in JSON; the other is the object. This guide explains what makes an array valid, how it differs from an array in a programming language, and what can break it when you copy, reformat or share it.

Is a JSON array the right format for your use?

A JSON array works well when one program hands an ordered list to another program, such as API responses, configuration values or exported records. Its main benefit is that almost every language has a standard JSON parser, so the list reads back with the same order and value types. The decisive tradeoff is strictness. One stray comma, a single-quoted string or a curly quote makes the whole document invalid, and JSON has no comment syntax for explaining entries.

Plain text is often the better choice when people are the main readers. That includes a list in an email, a chat message or a document, where no program needs to parse it. Plain text is also safer when the destination tends to alter punctuation, as many word processors do. Use JSON when machine readability matters. Use a plain list or a table when human readability matters more.

What a JSON array is, and the syntax rules that make it valid

The two rules people most often mean by "the syntax rules for a JSON array" are these: the array starts with [ and ends with ], and its elements are separated by commas. A valid array also has to follow the rules for the values inside it. The JSON standard (published as RFC 8259 and ECMA-404) defines these rules.

  • Brackets: an array opens with [ and closes with ]. An empty array, [], is valid.
  • Commas: put exactly one comma between elements, with no comma before the first element or after the last one.
  • Allowed values: strings, numbers, objects, arrays, and the literals true, false and null. Element types can be mixed.
  • Strings: always use double quotes. Escape a double quote or backslash inside a string with a backslash. Control characters such as a raw line break must be escaped, for example as \n.
  • Numbers: write them in decimal, with no leading plus sign and no leading zeros (0.5 is valid; 05 and .5 are not). NaN, Infinity and hexadecimal values are not allowed.
  • Literals: true, false and null must be lowercase.
  • Order: element order is meaningful and is preserved.
  • Whitespace: spaces, tabs, line feeds and carriage returns between elements are ignored.

These arrays are valid:

["red", "green", "blue"]

[1, "two", true, null, {"id": 3}, [4, 5]]

[]

Each line below contains a common error:

['red', 'green']          single quotes are not allowed
["red", "green",]         trailing comma
["red" "green"]           missing comma
[1, 2, NaN]               NaN is not a JSON value
[True, None]              Python-style literals, not JSON
["a", /* note */ "b"]     comments are not allowed

JSON array vs. an array in code

"JSON vs array" compares two different kinds of things. An array is a data structure that lives in a program's memory. JSON is a text format for writing data down. A JSON array is the text form of an ordered list. A parser turns that text into a native list or array, and a serializer turns the native structure back into text.

JSON is not a subset of every language's array syntax, so confusion is common. A JavaScript array literal can use single quotes, trailing commas, comments, undefined and empty slots. None of these is valid JSON. When JavaScript's JSON.stringify serializes an array, it writes undefined, NaN and Infinity as null. Python's repr of a list uses single quotes and True, False and None, so printing a Python list does not produce JSON. Use the language's JSON serializer instead of string formatting.

Feature | JSON array | JavaScript array literal
String quotes | Double only | Single, double or backtick
Trailing comma | Not allowed | Allowed
Comments | Not allowed | Allowed
undefined, NaN, Infinity | Not allowed | Allowed
What it is | Text for data exchange | Code inside a program

What formatting changes, and what it leaves alone

Pretty-printing and minifying change only the whitespace between tokens, so the meaning stays the same. These two arrays are equivalent:

[1,2,3]

[
  1,
  2,
  3
]

Some formatters do more than adjust whitespace: they parse the array and then serialize it again. This round trip can change how a value is written while keeping the value itself. For example, an escape sequence such as \u00e9 may come out as the literal character é. It can also change the value. RFC 8259 warns that numbers beyond the precision of an IEEE 754 double may not survive between implementations. A very large integer ID can be silently rounded, and 1.0 may be written back as 1. If exact numeric text matters, as with IDs, currency or hashes, store those values as strings or check that your tool preserves numbers. This article does not include tests of particular formatters, so check the tool you plan to use.

Keeping an array valid after copying or sharing

Most breakage happens in transit rather than during writing. Common causes include:

  • Curly quotes: some editors and word processors replace straight double quotes with typographic quotes, and parsers reject those.
  • Non-breaking or other Unicode spaces: JSON only treats space, tab, line feed and carriage return as whitespace, so a pasted non-breaking space between tokens causes a parse error.
  • Byte order mark: RFC 8259 says JSON text should not begin with a byte order mark (BOM). Parsers may ignore one, but some report an error.
  • Encoding: JSON exchanged between systems is expected to be UTF-8 under RFC 8259. Saving a file in another encoding can corrupt non-ASCII strings.
  • Truncation: a chat or message field with a length limit can cut off the closing bracket.

After pasting, run the text through a JSON parser or validator before relying on it. If the destination is a document or message, put the array in a code block or attach it as a .json file to reduce automatic punctuation changes. How well a particular platform preserves pasted text has not been verified here, so test your own destination.

Accessibility deserves a separate decision. A screen reader may read brackets, quotes and commas aloud, which can make long arrays tedious to follow. The exact behavior depends on the assistive technology and its punctuation settings, and no scoped test results are available here. When people need to understand the content, provide a plain-text list or table alongside the JSON rather than instead of it, so machines and readers each get a suitable version.

Quick checklist

  • Square brackets at both ends
  • One comma between each pair of elements, with none at the end
  • Double quotes around every string, with inner quotes and backslashes escaped
  • Lowercase true, false and null
  • No comments, NaN, Infinity or undefined
  • Large or exact-format numbers stored as strings if precision matters
  • Validated with a parser after any copy, paste or reformat