coderkid coderkid - 6 months ago 25
Node.js Question

Iterating through this JSON?

I'm new to JS, so this is a bit different than typical data structures I'm used to working with. Below is a simple snippet of code I'm trying to iterate through. I have put some sample code I have tried to at least dissect the information inside the JSON, but it's not working the way I want it to.

[{
"data": ["\t\tThe Catcher In The Rye by J. D. Salinger[Team Nanban][TPB]\n", "magnet:?xt=urn:btih:d87b74de181abda5dc54be34713473ae025e5ea1&dn=The+Catcher+In+The+Rye+by+J.+D.+Salinger%5BTeam+Nanban%5D%5BTPB%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
"epub_convert": 0
}, {
"data": ["\t\tThe Catcher In The Rye (Corrected and Updated)\n", "magnet:?xt=urn:btih:a5db6afb1eb238fa990b02377c38088e333609ad&dn=The+Catcher+In+The+Rye+%28Corrected+and+Updated%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
"epub_convert": 0
}, {
"data": ["\t\tTHE CATCHER IN THE RYE - J. D. Salinger.pdf\n", "magnet:?xt=urn:btih:6c146dd22323e604b74a326d23122a812a107466&dn=THE+CATCHER+IN+THE+RYE+-+J.+D.+Salinger.pdf&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
"epub_convert": 0
}, {
"data": ["\t\tCatcher In The Rye (Final Proof)\n", "magnet:?xt=urn:btih:faee3c58e847c0f2f8df00a3b5be870daa2e0dee&dn=Catcher+In+The+Rye+%28Final+Proof%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
"epub_convert": 0
}, {
"data": ["\t\tJ.D. Salinger:The Catcher in the Rye pdf, e-pub, mobi\n", "magnet:?xt=urn:btih:3ccfcbfbdf1d835776c4a1b02c60acf5f2dc036d&dn=J.D.+Salinger%3AThe+Catcher+in+the+Rye++pdf%2C+e-pub%2C+mobi&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
"epub_convert": 1
}]

I'm trying to iterate through this JSON, but can't seem to get the formatting correct. How would I be able to loop through getting the titles, links, and epub_convert values in a clean way?

#Starter Attempts
for (var i = 0; i < data.length; i++){
console.log(JSON.parse(data[i]));
}

________
for (var i = 0; i < data.length; i++){
console.log(data[i]);
}
_________

console.log(data[i]['data']['epub_convert']);





Output text from console.log



Object {readyState: 4, responseText: "[{"data":["\t\tThe Catcher In The Rye by J. D. Sal…F%2Fexodus.desync.com%3A6969"],"epub_convert":1}]", responseJSON: Array[5], status: 200, statusText: "OK"}

Answer

Just use forEach() to iterate over array elements

var data = [{
  "data": ["\t\tThe Catcher In The Rye by J. D. Salinger[Team Nanban][TPB]\n", "magnet:?xt=urn:btih:d87b74de181abda5dc54be34713473ae025e5ea1&dn=The+Catcher+In+The+Rye+by+J.+D.+Salinger%5BTeam+Nanban%5D%5BTPB%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tThe Catcher In The Rye (Corrected and Updated)\n", "magnet:?xt=urn:btih:a5db6afb1eb238fa990b02377c38088e333609ad&dn=The+Catcher+In+The+Rye+%28Corrected+and+Updated%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tTHE CATCHER IN THE RYE - J. D. Salinger.pdf\n", "magnet:?xt=urn:btih:6c146dd22323e604b74a326d23122a812a107466&dn=THE+CATCHER+IN+THE+RYE+-+J.+D.+Salinger.pdf&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tCatcher In The Rye (Final Proof)\n", "magnet:?xt=urn:btih:faee3c58e847c0f2f8df00a3b5be870daa2e0dee&dn=Catcher+In+The+Rye+%28Final+Proof%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tJ.D. Salinger:The Catcher in the Rye pdf, e-pub, mobi\n", "magnet:?xt=urn:btih:3ccfcbfbdf1d835776c4a1b02c60acf5f2dc036d&dn=J.D.+Salinger%3AThe+Catcher+in+the+Rye++pdf%2C+e-pub%2C+mobi&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 1
}];

data.forEach(function(v) {
  console.log(
    v.data[0], // title
    v.data[1], // url
    v.epub_convert // epub_convert value
  );
});


In case it's a string then you need to parse it first use JSON.parse() method for that.

var json = `[{
  "data": ["\\t\\tThe Catcher In The Rye by J. D. Salinger[Team Nanban][TPB]\\n", "magnet:?xt=urn:btih:d87b74de181abda5dc54be34713473ae025e5ea1&dn=The+Catcher+In+The+Rye+by+J.+D.+Salinger%5BTeam+Nanban%5D%5BTPB%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tThe Catcher In The Rye (Corrected and Updated)\\n", "magnet:?xt=urn:btih:a5db6afb1eb238fa990b02377c38088e333609ad&dn=The+Catcher+In+The+Rye+%28Corrected+and+Updated%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tTHE CATCHER IN THE RYE - J. D. Salinger.pdf\\n", "magnet:?xt=urn:btih:6c146dd22323e604b74a326d23122a812a107466&dn=THE+CATCHER+IN+THE+RYE+-+J.+D.+Salinger.pdf&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tCatcher In The Rye (Final Proof)\\n", "magnet:?xt=urn:btih:faee3c58e847c0f2f8df00a3b5be870daa2e0dee&dn=Catcher+In+The+Rye+%28Final+Proof%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tJ.D. Salinger:The Catcher in the Rye pdf, e-pub, mobi\\n", "magnet:?xt=urn:btih:3ccfcbfbdf1d835776c4a1b02c60acf5f2dc036d&dn=J.D.+Salinger%3AThe+Catcher+in+the+Rye++pdf%2C+e-pub%2C+mobi&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 1
}]`;

var data = JSON.parse(json);

data.forEach(function(v) {
  console.log(
    v.data[0], // title
    v.data[1], // url
    v.epub_convert // epub_convert value
  );
});


UPDATE : You need to parse it like JSON.parse(return_data.responseText), since the json string holds the responseText property.