To parse it you need to use double quotes instead of single.

This should work:

services = '["service1", "service2", "service3"]'
JSON.parse(services)
Answer from Erick R Soto on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ string-to-array-in-javascript
JavaScript - Convert String to Array - GeeksforGeeks
August 5, 2025 - The Array.from() method can convert a string into an array of characters. This method is especially useful if you want to create an array from an iterable object, like a string, without needing to specify a separator.
Discussions

How to convert Array formatted as String to Array?
JSON convert. var result = System.Text.Json.JsonSerializer.Deserialize(jsonString); More on reddit.com
๐ŸŒ r/learncsharp
6
5
March 22, 2023
javascript - Convert string with commas to array - Stack Overflow
In this case alert shows 0,1. If it where an array, it would show 0. And if alert(array[1]) is called, it should pop-up 1 ยท Is there any chance to convert such string into a JavaScript array? More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Converting String Array to an Integer Array - Stack Overflow
so basically user enters a sequence from an scanner input. 12, 3, 4, etc. It can be of any length long and it has to be integers. I want to convert the string input to an integer array. so int[0] w... More on stackoverflow.com
๐ŸŒ stackoverflow.com
help convert nested string array objects to array
JSON.parse is clever - I like that. You can shorten it a bit though with map: str = [ "[1, 2]", "[2, 3]", "[4, 6]" ] let strArrays = str.map(JSON.parse) More on reddit.com
๐ŸŒ r/learnjavascript
3
4
March 19, 2022
People also ask

Can we convert a string into a list instead of an array?
Yes, a string can be converted into a list by iterating over its characters or using built-in methods that transform it into a list of substrings or characters.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ string to array in java
Converting String to Array in Java: A Comprehensive Guide
How to handle empty strings when converting to an array?
You can handle empty strings by checking if the string is empty before conversion, setting a default value, or filtering out empty elements after conversion.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ string to array in java
Converting String to Array in Java: A Comprehensive Guide
What is a string in Java?
A string in Java is a sequence of characters enclosed within double quotes. String in Java are immutable that means once they are created their value canโ€™t change. Any modifications to a string results in a new string object being created. A string in Java is a sequence of characters enclosed within double quotes. String in Java are immutable that means once they are created their value canโ€™t change. Any modifications to a string results in a new string object being created. In Java, strings are fundamental data types used extensively in software development. However, there are scenarios where
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ string to array in java
Converting String to Array in Java: A Comprehensive Guide
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_howto_string_to_array.asp
Java How To Convert a String to an Array
Explanation: The method toCharArray() turns the string into an array of characters.
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ string-to-array-conversion-in-javascript
String to Array conversion in JavaScript | Board Infinity
June 13, 2026 - There is just a single method that is quite famous and widely used in JavaScript for turning a String into an array in JS and that is the .split() method. This method divides a string into an array of other strings, using a particular character, ...
๐ŸŒ
DEV Community
dev.to โ€บ sanchithasr โ€บ 6-ways-to-convert-a-string-to-an-array-in-javascript-1cjg
6 Ways to Convert a String to an Array in JavaScript - DEV Community
September 24, 2022 - Converting from string to array is always done using the split() method but after ES6, there are many tools we could do the same.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ string to array in java
Converting String to Array in Java: A Comprehensive Guide
August 27, 2025 - An array in Java is a data structure that stores multiple values of the same type in a contiguous memory location. Converting a string to array in java allows for easier processing, which includes extracting individual characters, splitting words, or performing operations like searching and sorting.
Find elsewhere
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ converting-arrays-and-strings-in-java
Java Array to String and String to Array Conversion Guide
In Java, strings are treated as objects, and the language furnishes a robust set of methods for the manipulation of textual information. This tutorial provides a comprehensive guide on how to convert arrays to strings and strings to arrays in Java.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ string-to-array-in-java-how-to-convert-a-string-to-an-array-in-java
String to Array in Java โ€“ How to Convert Strings to Arrays
July 6, 2023 - This method splits the string into individual characters and returns an array containing those characters. Store the resulting character array in the variable charArray. Iterate over the charArray using a for-each loop to print each character ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert string to array in java
String to Array in Java - Scaler Topics
April 19, 2024 - Create a character array that is the same length as the string. ... Return or perform the operation on the character array. ... To convert a string to a char array, we can use the toCharArray() function.
๐ŸŒ
arrayThis
arraythis.com
Online array converter; convert your text list to array | arrayThis
Instantly convert your text list to array using arrayThis online tool; valid arrays for JS, PHP, PERL, Python and much more.
๐ŸŒ
Reddit
reddit.com โ€บ r/learncsharp โ€บ how to convert array formatted as string to array?
r/learncsharp on Reddit: How to convert Array formatted as String to Array?
March 22, 2023 -

I'm given these strings of formatted arrays

"["cat","dog","horse"]"

"[1123,1234,2345]"

How do I format them into an array such that

arr[0] = "cat" arr[1] = "dog" arr[2] = "horse"

or

arr[0] = 1123 arr[1] = 1234 arr[2] = 2345

Top answer
1 of 6
79

You could read the entire input line from scanner, then split the line by , then you have a String[], parse each number into int[] with index one to one matching...(assuming valid input and no NumberFormatExceptions) like

String line = scanner.nextLine();
String[] numberStrs = line.split(",");
int[] numbers = new int[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++)
{
   // Note that this is assuming valid input
   // If you want to check then add a try/catch 
   // and another index for the numbers if to continue adding the others (see below)
   numbers[i] = Integer.parseInt(numberStrs[i]);
}

As YoYo's answer suggests, the above can be achieved more concisely in Java 8:

int[] numbers = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray();  

To handle invalid input

You will need to consider what you want need to do in this case, do you want to know that there was bad input at that element or just skip it.

If you don't need to know about invalid input but just want to continue parsing the array you could do the following:

int index = 0;
for(int i = 0;i < numberStrs.length;i++)
{
    try
    {
        numbers[index] = Integer.parseInt(numberStrs[i]);
        index++;
    }
    catch (NumberFormatException nfe)
    {
        //Do nothing or you could print error if you want
    }
}
// Now there will be a number of 'invalid' elements 
// at the end which will need to be trimmed
numbers = Arrays.copyOf(numbers, index);

The reason we should trim the resulting array is that the invalid elements at the end of the int[] will be represented by a 0, these need to be removed in order to differentiate between a valid input value of 0.

Results in

Input: "2,5,6,bad,10"  
Output: [2,3,6,10]

If you need to know about invalid input later you could do the following:

Integer[] numbers = new Integer[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++)        
{
    try 
    {
        numbers[i] = Integer.parseInt(numberStrs[i]);
    }
    catch (NumberFormatException nfe)   
    {
        numbers[i] = null;
    }
}

In this case bad input (not a valid integer) the element will be null.

Results in

Input: "2,5,6,bad,10"  
Output: [2,3,6,null,10]

You could potentially improve performance by not catching the exception (see this question for more on this) and use a different method to check for valid integers.

2 of 6
44

Line by line

int [] v = Stream.of(line.split(",\\s+"))
  .mapToInt(Integer::parseInt)
  .toArray();

With the Arrays.stream() alternative as

int [] v = Arrays.stream(line.split(",\\s+"))
  .mapToInt(Integer::parseInt)
  .toArray();

However much better for parsing bigger chunks of text is

int [] v = Pattern.compile(",\\s+").splitAsStream(line)
  .mapToInt(Integer::parseInt)
  .toArray();  

As this does not require the string array to be in memory, and we do an incremental parse to produce the integers. Moreover, as the input to splitAsStream is a CharSequece, and not just String, we can now also used buffered character sources to avoid even having the full input source in memory.

See also How do I create a Stream of regex matches? for some more interesting reading on incremental parsing.

๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_tostring_array.asp
JavaScript Array toString() Method
The toString() method returns a string with array values separated by commas.
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 83-4-ways-to-convert-string-to-character-array
4 Ways to Convert String to Character Array in JavaScript | SamanthaMing.com
Here are 4 ways to split a word into an array of characters. "Split" is the most common and more robust way. But with the addition of ES6, there are more tools in the JS arsenal to play with ๐Ÿงฐ ยท I always like to see all the possible ways to solve something because then you can choose the best way for your use case. Also, when you see it pop up in someone's codebase, you will understand it with ease ๐Ÿ‘โ€ฌ ยท const string = 'word'; // Option 1 string.split(''); // Option 2 [...string]; // Option 3 Array.from(string); // Option 4 Object.assign([], string); // Result: // ['w', 'o', 'r', 'd']
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ converting string to array in php using different methods
Converting String to Array in PHP Using Different Methods
September 17, 2025 - Understand the need to convert string to array in PHP and explore the complete list of methods used to convert a string to an array in PHP. Start learning now!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
CodingNomads
codingnomads.com โ€บ javascript-array-to-string-conversion
JavaScript Array-String Conversion
The output will be: Our team members are Alice, Bob, Charlie, and Dana. Next up, you'll be checking out the .split() method, which is like the inverse of .join(). It's used to convert a string to an array.
๐ŸŒ
Reddit
reddit.com โ€บ r/godot โ€บ converting a string to an array
r/godot on Reddit: Converting a String to an Array
October 24, 2023 -

I have a string that is something like ["Value1","Value2","Value3"]. I would like to convert this to an array. I'd like to know if there is an easy way to convert that is effectively an array in String format into an actual array.

I'd prefer to avoid making a big loop that inserts the values from the string into a new array with find and substr but if that's required then that can be done.

๐ŸŒ
airbrake
docs.airbrake.io โ€บ home โ€บ blog โ€บ javascript error handling โ€บ javascript: how to convert a string into an array
JavaScript: How To Convert a String Into an Array - Airbrake Docs
January 19, 2022 - The split() method is the most popular way to do this. The split() method is one of the most popular ways JavaScript developers convert their strings into arrays.