Parse a string into a JSON array

How to turn a string into a JSON array: when JSON.parse or json.loads is enough, when you need to split or use a CSV parser, and what breaks with Unicode and copy-paste.

Turning a string into a JSON array can mean two different jobs. Sometimes the string is already JSON text, such as ["red","green"], and you only need to parse it into a real array. Other times the string is ordinary delimited text, such as red, green, blue, and you need to split it and then serialize it as JSON. Which job you have decides which method is correct, and most failures come from treating one as the other.

Is this conversion suitable for your use?

Parsing is the right choice when the string came from a JSON source, such as an API response, a configuration value or a database column that stores serialized data. You get an actual array you can index, loop over and validate, and the parser rejects malformed input instead of guessing. That strictness is both the main benefit and the main tradeoff: hand-typed or pasted text often fails because of single quotes, trailing commas or typographic quotes.

Plain text is preferable when the value is only shown to people and never processed by code, when the list is a single human-written sentence, or when the data is really CSV with quoted fields. In that last case a CSV parser, not a JSON parser or a naive split, is the safer tool.

What the conversion actually changes

Parsing changes the stored data type, not just its appearance. Before parsing you hold one string of characters; after parsing you hold a structured value in memory. Serializing goes the other way: JSON.stringify or json.dumps turns the array back into text. Pretty-printing with indentation is purely presentational; it adds whitespace between tokens but does not change the parsed result.

Input string | What it is | Approach
["a","b","c"] | Valid JSON array text | JSON.parse (JavaScript) or json.loads (Python)
a,b,c | Delimited plain text | Split on the delimiter, trim items, then serialize if you need JSON text
"Smith, J","Lee, K" | CSV with quoted fields | Use a CSV parser; splitting on commas breaks the quoted values
['a','b'] | Not valid JSON (single quotes) | Fix the source, or in Python use ast.literal_eval for Python-literal input
"["a","b"]" | Double-encoded JSON | Parsing once returns a string; parse a second time to get the array
// JavaScript: string that is already JSON
const items = JSON.parse('["red","green","blue"]');
// items is an array: ["red", "green", "blue"]

// JavaScript: comma-separated plain text
const parts = "red, green, blue".split(",").map(s => s.trim());
const jsonText = JSON.stringify(parts);
// jsonText is the string: ["red","green","blue"]
# Python
import json

items = json.loads('["red", "green", "blue"]')
# items is a list: ['red', 'green', 'blue']

parts = [s.strip() for s in "red, green, blue".split(",")]
json_text = json.dumps(parts)
# json_text is the string: ["red", "green", "blue"]

Always confirm the result is actually an array. JSON.parse('"hello"') and JSON.parse('{"a":1}') both succeed but return a string and an object. In JavaScript, Array.isArray checks this; in Python, isinstance(value, list) does.

Validity rules that commonly cause errors

  • Strings must use double quotes; single quotes are invalid JSON.
  • Trailing commas after the last element are invalid.
  • NaN, Infinity and undefined are not valid JSON values. Python's json module accepts NaN and Infinity by default as an extension, so input that loads in Python may still fail in a strict parser.
  • In JavaScript, integers larger than Number.MAX_SAFE_INTEGER (9007199254740991) lose precision after parsing. Keep large IDs as strings if exact values matter.
  • Unescaped control characters, such as a raw newline inside a string value, are invalid; they must be written as escapes like \n.

Unicode: splitting a string into characters

A related task is turning a string into an array of its characters. JSON itself handles Unicode correctly, but the splitting method matters. In JavaScript, split('') divides by UTF-16 code units, which breaks emoji outside the Basic Multilingual Plane into unpaired surrogate halves. Array.from splits by code points, which keeps a single emoji intact but still separates a base letter from a combining accent or an emoji from its skin-tone modifier. Intl.Segmenter with grapheme granularity splits by user-perceived characters.

const s = "👍🏽";  // thumbs up + skin tone modifier: 2 code points

s.split("").length;        // 4 (UTF-16 code units, broken halves)
Array.from(s).length;      // 2 (code points, modifier separated)
[...new Intl.Segmenter("en", { granularity: "grapheme" }).segment(s)].length;  // 1

const e = "e\u0301";  // e followed by a combining acute accent
Array.from(e).length;      // 2, although it displays as one letter

If the array will be shown to people or used for character counts, use grapheme segmentation. If it feeds a byte- or code-point-level process, code points may be what you want. Decide deliberately rather than relying on the default split.

Will the result stay usable after copying or sharing?

JSON text copied from documents, chat apps or web pages is a frequent source of parse errors. Typographic quotes that some editors substitute for straight double quotes are not valid JSON delimiters. Non-breaking spaces outside string values are not JSON whitespace, which allows only space, tab, line feed and carriage return. A leading byte order mark is rejected by JSON.parse and by Python's json.loads when present in the string. These characters are often invisible, so an error pointing at an apparently correct position is a sign to check for them.

For readability, minified single-line JSON is hard to scan or review. Pretty-printing, for example JSON.stringify(value, null, 2) or json.dumps(value, indent=2), makes structure visible without changing the data. When sharing a parsed list with non-technical readers, a plain-text or bulleted version is usually clearer than raw JSON, and screen readers read bracket and quote punctuation inconsistently depending on settings. No assistive-technology testing was available for this article, so treat that as a general caution rather than a measured result.

Online converters and formatters can be convenient, but their appearance or marketing says nothing reliable about whether input is logged or retained. For data containing credentials, personal information or internal records, parsing locally in your own code or editor avoids that question.

Recommendation

If the string is valid JSON, use the language's standard parser and verify the result is an array. If it is delimited text, split and trim, then serialize; switch to a CSV parser as soon as fields can contain the delimiter. Clean pasted input of typographic quotes, non-breaking spaces and byte order marks before parsing, and choose grapheme-aware splitting when the array represents visible characters. Keep plain text when the content is only for human reading.

Evidence gap: no specific tools, converters or environments were tested for this article. The behavior described reflects documented parser and language rules; results in a particular online tool or editor should be checked directly.