0

Am trying to get the key values from a josn response..

This is the response data obtained.from this i need to get the projects values

{ '$':
   { id: 'TP',   

     href: '/app/rest/projects/id:yuioTP',
     webUrl: 'http://teamcity.jffjf
' },
  parentProject: [ { '$': [Object] } ],
  buildTypes: [ { '$': [Object] } ],
  templates: [ { '$': [Object], buildType: [Object] } ],
  parameters: [ { '$': [Object], property: [Object] } ],
  vcsRoots: [ { '$': [Object] } ],
  projects: [ { '$': [Object], project: [Object] } ] }

code

var parseString = require('xml2js').parseString;
var async  = require('async');

var getJson = function(callback) {
    http.get(options, function(res) {
        var data = '';
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            data += chunk.toString();
        });
        res.on('end', function() {
            var output = {};
            var err = null;
            try {

                parseString(data, function(err, result) {
                    async.each(result, function(test, test_callback) {
                        console.log(test.projects)

                    }, function(err) {
                        callback(null, report);
                    });
                });
            } catch (e) {
                err = e;
            }
            //  callback(err, output);
        })
    }).on('error', function(err_) {

        console.log(err_, url);
        callback({
            error: err_
        });
    });
}

getJson();


 console.log(test.projects)

// Prints ....How can i get the object data inside the projects key

 [ { '$': { count: '9' },
    project:
     [ [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object] ] } ]
2
  • Have you tried Object.keys? Commented Apr 6, 2016 at 9:37
  • i dont know how to use that..i didnmt tried Commented Apr 6, 2016 at 9:39

2 Answers 2

1

Using Object.keys method. Simple example below.

var a = {
  foo: "hello",
  baz: "world"
}

var array = [a,a,a,a,a] 

array.forEach(function(item){
  Object.keys(item).forEach(function(key){
    console.log(key + ' : ' + item[key])
  })
})

In your case you should iterate through <object>.projects and pass each item into Object.keys method

<object>.projects.forEach(function(item){
  Object.keys(item).forEach(function(key){
     console.log(key + ' : ' + item[key])
  })
}

I hope it will help you fiddle.

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

1 Comment

it returs project : [object Object] only
0

Try this:

projects[0].project.forEach(function(item){

  console.log(item[0]); //log whole object
  var temp = item[0];
  Object.keys(temp).forEach(function(key){
    console.log(key + ' : ' + temp[key]); //log value by key
  });
});

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.