Reputation: 3
I query two arrays from the database and turn them into json format. The set up is like this:
{"groups":"[apple,bee,car,dogs,egos,fruits]", "members":"[g,h,i,j,k,l]"}
I am trying to access each element in groups. The groups array is a list. I tried using index, and it's returning me groups[0] = 'a', groups[1] = 'p'... So using index doesn't work.
I also want to count how many elements in the groups array or in the members array, and using .length only give me back the character length, not the actual numbers of elements.
Any advice would be appreciated. Thanks.
Upvotes: 0
Views: 1174
Reputation: 469
It doesn't work because all elements of the array are in one string.
As has been mentioned this not correct JSON formatting, but sometimes you don't have control over how you get information. So although this is not a recommended answer, you could use simple string parsing to get back the values you want by doing, something like:
var stuff = JSON.parse('{"groups":"[apple,bee,car,dogs,egos,fruits]", "members":"[g,h,i,j,k,l]"}');
var groups = stuff.groups.split("[")[1].split("]")[0].split(",");
var members = stuff.members.split("[")[1].split("]")[0].split(",");
console.log(groups); // ["apple", "bee", "car", "dogs", "egos", "fruits"]
console.log(groups[1]); //bee
console.log(members[0]); //g
I would like to reiterate this is not an ideal solution, but sometimes it is all you can do.
Upvotes: 0
Reputation: 1087
You should consider constructing the array differently from the database.
You are getting those letters because they are in the position in the string that you are referencing with the index.
Consider using regex and the split()
function to parse them as they are now:
var obj = {"groups":"[apple,bee,car,dogs,egos,fruits]", "members":"[g,h,i,j,k,l]"};
// Regex replaces the square brackets with blank spaces and splits by the
// comma into an array
var groups = obj.replace(/[\[\]']+/g,'').split(',');
var members = obj.replace(/[\[\]']+/g,'').split(',');
Now groups[1]
will return 'bee'.
Read more about split()
here.
Upvotes: 0
Reputation: 970
JSON.parse(groups)
will not work, because [apple,bee,car,dogs,egos,fruits]
is not correct JSON string.
["apple","bee","car","dogs","egos","fruits"]
- is correct JSON string, that can be parsed.
P.S. members
is not correct JSON string too.
// If you have some data
data = {
groups: ["apple", "bee", "car", "dogs", "egos", "fruits"],
members: ["g", "h", "i", "j", "k", "l"]
};
// you can convert it to JSON string
jsonData = JSON.stringify(data);
console.log('JSON data: ', jsonData);
// and then parse this string
restoredData = JSON.parse(jsonData);
// after this you can access object members again
console.log('groups[0]: ', restoredData.groups[0]);
Upvotes: 4
Reputation: 894
This is happening because "[apple,bee,car,dogs,egos,fruits]"
it's an string. You have to parse it before accessing the element.
Let's say that you have the JSON in the variable test
, then we have to delete [
and ]
and split the string like this:
test.groups = test.groups.replace("[", "")
test.groups = test.groups.replace("]", "")
test.groups = test.groups.split(',')
And then now it contains:
["apple", "bee", "car", "dogs", "egos", "fruits"]
Upvotes: 1