0

What I thought was going to be a simple question has turned out to more difficult than expected. Without complicating matters, I have this JSON string that is being returned from the server. This is exactly what the string looks like. It is inside data.occupation.

{"occupation": "Boxer", "id": 2},{"occupation": "Helper", "id": 3}

What I need is an array of ids: [2, 3]

I have tried to loop over this set and get type error, undefined.

Is it even possible to do this with JQuery, or should I be parsing this backend and sending JQuery an array of ids?

Solver
  • 618
  • 7
  • 15
NaN
  • 1,286
  • 2
  • 16
  • 29
  • One solution would be to do something like - ```data.occupation.map(function(o) { return o.id });``` – Bogdan Martinescu Dec 24 '17 at 18:14
  • Thank guys. I suck with Javascript. Can you please provide an example for me to follow? I've already tried map but could not get that to work. Please, I've been stuck on this for 3 hours – NaN Dec 24 '17 at 18:16
  • I think you need to use ```var json = JSON.parse(data.occupation)``` on the JSON first and then map over it – Bogdan Martinescu Dec 24 '17 at 18:18
  • @BogdanMartinescu, when I use that, I get `SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 46 of the JSON data` – NaN Dec 24 '17 at 18:19
  • oh, is that in an array or not, it doesn't seem so from your code – Bogdan Martinescu Dec 24 '17 at 18:20
  • It is not an array. That's the thing. When I log `data.occupation`, what I posted is exactly what I get back. I don't know if I need to create an array and dump the string into it, or what. – NaN Dec 24 '17 at 18:24
  • try ```Array.of(data.occupation)``` or ```Array.from(data.occupation)``` then map over it – Bogdan Martinescu Dec 24 '17 at 18:28
  • 2
    Possible duplicate of [Parse JSON in JavaScript?](https://stackoverflow.com/questions/4935632/parse-json-in-javascript) – ashleedawg Jan 03 '18 at 14:51

1 Answers1

3

You don't need jQuery for this.

First of all, if you have a string, you should parse it.

var jsons = JSON.parse(your_string)

Now, just loop through all of your JSONs and add them to an array.

var ids = []
for (var i = 0; i < jsons.length; i++) {
    ids.push(jsons[i].id)
}

Edit: if your data is exactly the data above, it won't parse as the JSONs must be in an array. Use this instead:

var jsons = JSON.parse("[" + your_string + "]")

This will put it into an array.

Solver
  • 618
  • 7
  • 15
  • Thank you, but I've already tried using JSON.parse and I get the following error: `SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 46 of the JSON data` – NaN Dec 24 '17 at 18:17
  • Can you show the JSON data in question? I can't help till I know this. – Solver Dec 24 '17 at 18:21
  • I can, but the Inspector in FF truncates everything. I can give you what it shows me though. – NaN Dec 24 '17 at 18:22
  • This is what I am able to pull from it: `Object { name: "Alex Barma", occupations: "{"occupation": "Boxer", "occup…", 6 more… }` – NaN Dec 24 '17 at 18:22
  • Is this a string? Edit - ah, you need to parse data.occupations; can you log `JSON.stringify(data)`? – Solver Dec 24 '17 at 18:25
  • I just learned how to tell if something is a string or not today, and yes, this is a string. I also just added the brackets around the string and now I am **FINALLY**, after 4 hours, seeing something that I may just be able to parse now. I see an array with objects in it. Should I use map with the results? – NaN Dec 24 '17 at 18:28
  • Yes, use map, or use the loop I show above, whichever you prefer ;) – Solver Dec 24 '17 at 18:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161907/discussion-between-solver-and-nan). – Solver Dec 24 '17 at 18:30