Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have an array which contains values as follows:

{
"123456": {
    "name": "tom",
    "projects": {
        "987654": {
        "cli": "abcd",
        "org": "123456",
        "cli_e": "abcd",
        "pro": "abcd",
        "status": "6"
        }
      }
   }
},
{
"654321": {
    "name": "jerry",
    "projects": {
        "123": {
        "cli": "xyz",
        "org": "000",
        "cli_e": "xyz",
        "pro": "xyz",
        "status": "3"
        }
      }
   }
}

I want output to be like below:

{
  "cli": "abcd",
  "org": "123456",
  "cli_e": "abcd",
  "pro": "abcd",
  "status": "6"
},
{
  "cli": "xyz",
  "org": "000",
  "cli_e": "xyz",
  "pro": "xyz",
  "status": "3"
}

How to do that?

share|improve this question
1  
Have you tried this: data['123456'].projects['987654']? – VisioN Jun 2 '14 at 11:09
    
Can you flesh out the question a bit more ? Are you saying 123456 is random and you won't know the "name" of the first item in the object ? Otherwise if you know the property names, then VisioN's answer will do it – user2808054 Jun 2 '14 at 11:12

1 Answer 1

up vote 1 down vote accepted

Got the answer by hsz

var output = [];
for (var k in input) {
  for (var kk in input[k].projects) {
    output.push(input[k].projects[kk]);
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.