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 OverflowTo parse it you need to use double quotes instead of single.
This should work:
services = '["service1", "service2", "service3"]'
JSON.parse(services)
This is used to convert string which is array into pure array
var a = '[Aakash,akash]'
a.replace(/\[|\]/g,'').split(',')
(2) ["Aakash", "akash"]
javascript - Convert string with commas to array - Stack Overflow
How to convert Array formatted as String to Array?
javascript - Convert string array representation back to an array - Stack Overflow
Converting a String to an Array
For simple array members like that, you can use JSON.parse.
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
[0, 1]
If you use .split(), you'll end up with an Array of strings.
["0", "1"]
Just be aware that JSON.parse will limit you to the supported data types. If you need values like undefined or functions, you'd need to use eval(), or a JavaScript parser.
If you want to use .split(), but you also want an Array of Numbers, you could use Array.prototype.map, though you'd need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Split it on the , character;
var string = "0,1";
var array = string.split(",");
alert(array[0]);
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
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.
So im trying to convert an array that was turned into a string from DynoDB. I got it formatted properly but I cant get it to work. (Incase you are wondering what this is for, its for a log keeping track of changes to a db. This is just showing the string with its state)
Here is the string:
"{PK:PHOTO01H70M6DC9YW6KPJBF15DQKDBF, SK:PHOTO01H70M6DC9YW6KPJBF15DQKDBF, type:photo, date:2023-08-02, dateRangeStart:null, dateRangeEnd:null, year:2023, featured:null, title:Title test, description:Adfsdf, linkAddress:null, location:Alberta, tags:[], people:[Free Press, School Division], bucket:bucket, region:null, key:null, filename:Screenshot 2023-07-19 at 13-44-34 Admin Add Files.png, filedesc:null, dateSK:2023-08-02#dodeyby, uploadedBy:deathbyunknown, createdAt:2023-08-04T15:55:54.644Z, updatedAt:2023-08-04T15:58:20.579Z}"
Things I've tried:
Convert using Object.entrys(), Result is an array with all the characters as a single item in the array.
JSON.parse(), Result error message: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data.
EDIT: Santizied Information that shouldn't be shown