0

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.

2
  • It sounds like you may not have parsed the json correctly into an array of strings Commented Jul 11, 2016 at 23:04
  • JSON supports arrays, there is no reason to try and add your values as strings. Commented Jul 11, 2016 at 23:25

4 Answers 4

4

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]);

Sign up to request clarification or add additional context in comments.

8 Comments

You are right, I have added a possible solution in my answer.
It's a valid JSON string, just an invalid array when parsed as one. ["apple","bee","car","dogs","egos","fruits"] isn't a JSON string, just an array value.
I agree with @SpencerWieczorek. I just mean that it's not valid JSON string of array that OP want to restore.
@LeonidZakharov Since it's an array when I query out from the database, how do I add the double quotes into each element of the array??
@moffittime Can you show the code the returns the JSON
|
1

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"]

Comments

0

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.

2 Comments

You know a lot about perversions ))
No, it's quite literal.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.