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 - Converting string-array to an array of strings - Stack Overflow
Converting a String Array into an Array
javascript - Convert string with commas to array - Stack Overflow
Convert array of key:value pair objects to string
JSON.parse would work, if the JSON wasn't invalid. As mentioned in the comments, the string would have to be formatted as '["1", "2"]'.
If you do not have control over the formatting, you can parse it manually: if the strings don't contain quotes, you can use #replaceAll("'", '"').
If you have edge cases you need to cover, json5 may be able to help: JSON5.parse(str) once you have the script loaded via NPM or unpkg
JSON only recognizes the usage of " ". To parse it you need to use double quotes instead of single quotes.
const myString = "['one', 'two']";
// change to
const myString = '["one", "two"]';
Heres an example:
// Not working
const Str = "['one', 'two']";
console.log(Str); // ['one', 'two']
console.log(typeof Str); // string
JSON.parse(Str); // SyntaxError: Unexpected token ' in JSON at position 1
// Working example:
const myString = '["one", "two"]';
console.log(myString); // ["one", "two"]
console.log(typeof myString); // string
const myArray = JSON.parse(myString);
console.log(myArray); // [ 'one', 'two' ]
console.log(typeof myArray); // object
In the case that you need to change ' to " just use replace.
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
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]);