0

I have a problem with getting some JSON fields in NodeJS. Here is my JSON structure:

{
    "header":
    {
        "file1":0,
        "file2":1,
        "subfiles":{
          "subfile1":"true",
          "subfile2":"true",
        }
    },

    "response":
    {
        "number":678,
        "start":0,
        "docs":[
            {
                "id":"d3f3d",
                "code":"l876s",
                "country_name":"United States",
                "city":"LA"
            },
            {
                "id":"d2f2d",
                "code":"2343g",
                "country_name":"UK",
                "city":"London"
            }
        ]
    }
}

How to access to specific fields (id, city or country_name) in structure like this? I'm trying something in NodeJS, but I cannot get specific fields.

request('http://url.com', function (error, res, body) {
  if (!error && res.statusCode == 200) {
    console.log(body) 
  }

    res.on('data', function(chunk){
        body += chunk;
    });

    res.on('end', function(){
        var urlResponse = JSON.parse(body);
        console.log("Got a response: ", urlResponse.response.docs.city);
    });
});
corry
  • 1,457
  • 7
  • 32
  • 63

2 Answers2

1

Try using using a data variable to accumulate your chunks and then data.toString() to force the buffered content to string, which will be parseable JSON.

Additionally, you need to access the specific member of the docs array.

Josh
  • 694
  • 7
  • 10
  • Can I use JSON.stringify(body) instead of toString()? – corry Jan 22 '16 at 12:54
  • No, because the content is Buffered. You will need to convert that buffered content to string before parsing as JSON. – Josh Jan 22 '16 at 12:54
  • Can you accept this answer if it solves your issue @corry ? – Josh Jan 22 '16 at 14:29
  • Your answer has helped, but I still didn't solve the problem. Thanks – corry Jan 22 '16 at 15:02
  • Please update your example and explain where you are still having issues. @corry – Josh Jan 22 '16 at 15:38
  • I still can't get value from array. I gave up from implementing this solution and I'm trying [this solution](http://stackoverflow.com/questions/34950357/store-json-data-in-mysql-table?noredirect=1#comment57632549_34950357) Thank you – corry Jan 22 '16 at 15:44
  • The above solution should work. In fact I have just completed a similar piece of code. How are you attempting to access your array elements? The method used above is not accurate. If you wanted to access a particular member of your array you would do so like this `urlResponse.response.docs[0].city` or you could use one of the loop examples suggested by others here. The solution you've chosen on the other SO question is definitely not standard. Good luck @corry! Let me know if you'd like to further explore this one. – Josh Jan 22 '16 at 16:33
1

Have you tried something like this:

thatObject.response.docs.forEach((element) => { console.log( element.id ); });

?

property docks is an array, so you can use functions: forEach, or for, or whatever you wish

  • I'm trying with `for(var i = 0; i < data.response.docs.length; i++){ var post = data.response.docs[i]; console.log(post); }` and I got Cannot read property 'docs' of undefined error – corry Jan 22 '16 at 13:40
  • 1
    Hey! It looks like you use request module? I have never used it, but try something like this: (how to start writing a new line here?;)) replace `data` with `body`. – Adam Paulukanis Jan 24 '16 at 08:54