Nick Nick - 3 months ago 7
JSON Question

How to extract info from json output

I have a json output that looks like this..

{
"XXXX": {
"name": "Dalvin Nieger",
"work": [
{
"department": {
"name": "Sales"
},
"start_date": "0000-00"
}
],
"link": "http://www.my-site.com/profile.php?id=XXXXX",
"id": "XXXXXX"
},
"XXXXXXX": {
"name": "Nick Mercer",
"work": [
{
"department": {
"name": "Marketing"
},
"start_date": "0000-00",
"end_date": "0000-00"
}
],
"link": "http://www.my-site.com/profile.php?id=XXXXXX",
"id": "XXXXXX"
}
}


Where XXXX is the id no. of the employee. I want to loop through the data and get id no., name, department he works in and end date for each employee using javascript.

I appreciate any help.

Thanks.

Answer

Your JSON isn't correct - it's wrapped in an array but you don't use keys in JSON arrays. If you changed the outer brackets ([) to braces ({) you'd just be able to loop through the JSON object keys in JavaScript. See here for instructions.