3

I have an array of JSON in ajax response in javascript.

[{"id":1,"text":"apple"},{"id":2,"text":"mango"},{"id":3,"text":"banana"}]

I want to extract a comma separated list of ids like "1, 2, 3" from this JSON response. How can i do this?

2
  • Have you tried anything yet? why is it not working? are you getting any errors, etc... Commented Mar 13, 2013 at 13:21
  • 1
    I don't have any idea to do this. Commented Mar 13, 2013 at 13:22

4 Answers 4

3

First, you parse the JSON (if you didn't do that already):

var arr = JSON.parse('[{"id":1,"text":"apple"},{"id":2,"text":"mango"},{"id":3,"text":"banana"}]');

Then you loop the array to extract the id on each object. You can use the map method of arrays to loop and add the ids to a new array in one go:

var ids = arr.map(function(item) {
    return item.id;
});
alert(ids.join(','));
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming your json is stored as a string jsonstr in your js code:

/*parse the JSON to a JS object*/
var data = JSON.parse(jsonstr); 
var ids = [];

for(var i=0; i<data.length; i++){

   //loop over the array and if the id is defined add it
   if(typeof data[i].id !== "undefined"){
       ids.push(data[i].id);
   }
}

4 Comments

@bfavaretto good question. Doesn't like comments at all apparently
@bfavaretto go for it. I just tried about 5 different things, any combination with comments, even if I use /**/ on its own line, breaks the code that comes after.
I just posted a question on meta about it: meta.stackexchange.com/questions/171669/…
@bfavaretto and it got fixed quickly. Thanks.
0
var a = [{
    "id": 1,
    "text": "apple"
}, {
    "id": 2,
    "text": "mango"
}, {
    "id": 3,
    "text": "banana"
}];
var ar = [];
for (var i = 0; i < a.length; i++) {
    ar.push(a[i].id);
}

alert(ar.join(','))

http://jsfiddle.net/t3w5S/1/

3 Comments

Thats not a JSON string you're defining, its a javascript object.
@ben336: correct me if i am wrong, original poster has already said he has "an array of JSON" and i intrepreted it as being parsed JSON in array form already
But he's got all his properties double quoted. Looks much more like a JSON string. Its always possible he's confused, but he's saying that he has JSON, and he's formatted it like its JSON so I don't think there's much reason to think its not JSON
-1

About this:

x = [{"id":1,"text":"apple"},{"id":2,"text":"mango"},{"id":3,"text":"banana"}];
str = "";
for(i=0;i<x.length;i++)
{
  str += x[i].id.toString() + ',';
}
str = str.substring(0, str.length - 1);
console.log(str);

1 Comment

Not defining variables with "var". Assuming an "id" exists. Calling toString unnecessarily when it is already being concentrated around two strings. If any one "id" is undefined it won't have a toString method and this snippet will error. Original poster said they wanted it concentrated by a comma plus a space. I see no space. Also using an overly complicated suspicious-looking substring approach to trim off the last character when a simple array and join would do the trick. And finally assuming the response is already an array when the Original Poster mentioned JSON which needs to be parsed.

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.