I have the following recursive function saved in a file called helpers.js. When it is loaded into the main app.js file using:
var helpers = require('./helpers');
calling it only works partially. The line:
s+=recurseJSON(o[a]);
doesn't get called so the JSON parsing doesn't recurse into nested levels.
I have also tried the following which still doesn't work:
s+=helpers.recurseJSON(o[a]);
If I move the code below into the main app.js file, the recursion works perfectly, obviously changing
recurseJSON: function(o) {...
to
function recurseJSON(o) {..
Your thoughts are appreciated. Here is the whole code:
module.exports = {
recurseJSON: function(o){
var s = '';
for(var a in o){
if (typeof o[a] == 'object'){
s+=a+':';
console.log('JSON>', a, ":");
s+=recurseJSON(o[a]); // This line should recurse but doesn't
}else{
s+=a+':'+o[a]+' ';
console.log('JSON>', a, ":", o[a]);
}//end if
}//end for
return s;
}
};
PS: Credit to Recursively parsing JSON for the original recursive code.