0

I have 7MB of .json file and I am filtering the data with my node.js and storing the data into a displayMe array. When I display that shorted array in console it's visible in that. But when I try to write that array using JSON.stringify. I get a json file with blank array.

Here is the 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;
for (var i = 0; i <len; i++) {

    for (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");// this part not working.

console.log(displayMe);//this is working

This is the 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 }
    ]
7
  • Any errors you see in console? Try this npmjs.com/package/jsonfile Commented May 9, 2016 at 10:52
  • 2
    Btw, your JSON is invalid. You can't have single quotes, nor have objects key/values as items in an array (they must be in an object). Commented May 9, 2016 at 10:53
  • Yep - incorrect JSON, try an online JSON validator. jsonlint.com Commented May 9, 2016 at 10:55
  • [ "key": { value: 0 } ] is invalid. Try something like: [ { "key": { value: 0 } } ] instead. Commented May 9, 2016 at 10:55
  • 2
    @evolutionxbox More like [ { "key": { "value" : 0 } } ] Commented May 9, 2016 at 11:02

3 Answers 3

1

The problem was the above js code was not generating array in proper JSON format. I tried and fixed that. here is 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;
for (var i = 0; i <len; i++) {
     var obj2={};//changes here
    for (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 was the few changes. It is working now.

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

Comments

0

The problem is caused by this line:

var displayMe = [];

You're initializing displayMe to be an empty array. However, you are subsequently treating that array as an object:

displayMe[myData[i]["Area Name"]] = (obj);

This won't stringify to JSON properly, because arrays are supposed to have particular properties (like length) that JSON.stringify() depends on.

Instead, initialize displayMe to be an empty object:

var displayMe = {};

Comments

0

Quote: "But when I try to write that array using JSON.stringify. I get a json file with blank array."

That's because JSON doesn't seem to parse or stringify named properties of an Array hybrid.

Your array length is 0. And the result will most certainly be = [].

You will have to replace Array:

[ 'State - JAMMU [...] Persons': 104099452 }  ]

with a proper Object:

{ 'State - JAMMU [...] Persons': 104099452 }  }

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.