javascript - Not able to generate JSON file using JSON.stringify() -


i have 7mb of .json file , filtering data node.js , storing data displayme array. when display shorted array in console it's visible in that. when try write array using json.stringify. json file blank array.

here code:

var fs = require("fs");  var data = fs.readfilesync('india2011.json');  var mydata=json.parse(data);//contains main array  var len=mydata.length;//main array length  var k=1; var count=0;  var displayme=[]; var canread=0; var cannotread=0; (var = 0; <len; i++) {      (var j = k; j <=35; j++) {         var obj={};      if ((mydata[i]["state code"]==(j))&&(mydata[i]["total/ rural/ urban"]=="total")&&(mydata[i]["age-group"]=="all ages")) {          obj["literate - persons"]=parseint(mydata[i]["literate - persons"]);         obj["illiterate - persons"]=parseint(mydata[i]["illiterate - persons"]);         obj["total - persons"]=parseint(mydata[i]["total persons"]);         //sort.push(obj);         displayme[mydata[i]["area name"]]=(obj);         count+=(parseint(mydata[i]["total persons"]));         canread+=(parseint(mydata[i]["literate - persons"]));         cannotread+=(parseint(mydata[i]["illiterate - persons"]));         ++k;         };     }; };  fs.writefile( "displayme.json", json.stringify( displayme ), "utf8");// part not working.  console.log(displayme);//this working 

this console output:

[ 'state - jammu & kashmir': { 'literate - persons': 7067233,     'illiterate - persons': 5474069,     'total - persons': 12541302 },   'state - himachal pradesh': { 'literate - persons': 5039736,     'illiterate - persons': 1824866,     'total - persons': 6864602 },   'state - punjab': { 'literate - persons': 18707137,     'illiterate - persons': 9036201,     'total - persons': 27743338 },   'state - chandigarh': { 'literate - persons': 805438,     'illiterate - persons': 250012,     'total - persons': 1055450 },   'state - uttarakhand': { 'literate - persons': 6880953,     'illiterate - persons': 3205339,     'total - persons': 10086292 },   'state - haryana': { 'literate - persons': 16598988,     'illiterate - persons': 8752474,     'total - persons': 25351462 },   'state - nct of delhi': { 'literate - persons': 12737767,     'illiterate - persons': 4050174,     'total - persons': 16787941 },   'state - rajasthan': { 'literate - persons': 38275282,     'illiterate - persons': 30273155,     'total - persons': 68548437 },   'state - uttar pradesh': { 'literate - persons': 114397555,     'illiterate - persons': 85414786,     'total - persons': 199812341 },   'state - bihar': { 'literate - persons': 52504553,     'illiterate - persons': 51594899,     'total - persons': 104099452 }     ] 

the problem above js code not generating array in proper json format. tried , fixed that. here working code:

var fs = require("fs");  var data = fs.readfilesync('india2011.json');  var mydata=json.parse(data);//contains main array  var len=mydata.length;//main array length  var k=1; var count=0;  var displayme=[]; var canread=0; var cannotread=0; (var = 0; <len; i++) {      var obj2={};//changes here     (var j = k; j <=35; j++) {         var obj={};         var header;//changes here      if ((mydata[i]["state code"]==(j))&&(mydata[i]["total/ rural/ urban"]=="total")&&(mydata[i]["age-group"]=="all ages")) {          obj["literate - persons"]=parseint(mydata[i]["literate - persons"]);         obj["illiterate - persons"]=parseint(mydata[i]["illiterate - persons"]);         obj["total - persons"]=parseint(mydata[i]["total persons"]);          header= mydata[i]["area name"];//changes here         obj2[header]=obj;//changes here          displayme.push(obj2);//changes here         count+=(parseint(mydata[i]["total persons"]));         canread+=(parseint(mydata[i]["literate - persons"]));         cannotread+=(parseint(mydata[i]["illiterate - persons"]));         ++k;         //console.log("addesd "+mydata[i]["total persons"]+" time j: "+k+" state name"+mydata[i]["area name"]+" i:"+i+"k:"+k+" count "+count);         };     }; };  fs.writefile( "displayme.json", json.stringify( displayme ), "utf8");  console.log(displayme); 

that few changes. working now.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -