0

I am reading data from a simple csv file and adding some data from a web service into an array using Node JS fs. The array is defined as:

var allDataOut=[];

The addition of the data happens at this point in the code:

var cumulate = {
            "Details":augment,
            "Timestamp": new Date(),
            data:data
        }
        allDataOut.push(cumulate);
        console.log(allDataOut);
        callback(cumulate);

According to the console.log I am seeing exactly what I would expect with the correct data sitting inside the [] denoting an array.

So to create a JSON I would expect the JSON.stringify(allDataOut) would give me the correct result.

console.log("writing file");
        var str = JSON.stringify(allDataOut);
        console.log("This is output .stringify" + str);

        fs.appendFileSync(outputFile, str, encoding='utf8');

but as you can see in the final data below it is still in the array format. I have seen a number of questions on this subject in this forum with one suggestion of using JSON.parse(JSON.stringify(allDataOut)); - unfortunately nothing seems to change the output.

[ {
    "Details": {
      "Name": "Tophat",
      "SegmentID": "0",
      "DistanceToDepot": "256.53"
    },
    "Timestamp": "2016-04-10T11:40:03.291Z",
    "data": {
      "metadata": {
        "language": "en-US",
        "transaction_id": "1460288403143:279662660",
        "version": "1",
        "Altitude": 49.44
      },
      "observation": {
        "class": "observation",
        "expire_time_gmt": 1460289003,
        "metric": {
          "wspd": 21,
          "gust": null,
          "vis": 9.7
        }
      }
    }   },   {
    "Details": {
      "Name": "Tophat",
      "SegmentID": "25659",
      "DistanceToDepot": "0"
    },
    "Timestamp": "2016-04-10T11:40:40.428Z",
    "data": {
      "metadata": {
        "language": "en-US",
        "transaction_id": "1460288440297:-647605523",
        "version": "1",
        "Altitude": 50.68
      },
      "observation": {
        "class": "observation",
        "expire_time_gmt": 1460288452,
        "metric": {
          "wspd": 18,
          "gust": null,
          "vis": 12.91
        }
      }
    }   } ]
3
  • What's the problem the data is valid json. Do you maybe want a JSON object which contains a json array. You can validate json on jsonformatter.curiousconcept.com Commented May 10, 2016 at 7:47
  • I am seeing exactly what I would expect with the correct data sitting inside the [] denoting an array then but as you can see in the final data below it is still in the array format. You're expecting an array then you're surprised when you get one? Commented May 10, 2016 at 8:40
  • Thanks for the clarification. It is a json array which I thought the json.stringify would convert to a row by row series of json objects. I am told the application used to read this file will read a json object but not an array. I'll research how to convert a json array to json objects. Commented May 11, 2016 at 0:50

1 Answer 1

0

So to suit the nature of the application reading the data the format need to have the data in a line by line. Being new to JSON I believe this does not create a JSON array per se but a series of JSON objects inside the file. And learnt how to do an EOL and iterate in Node JS. To achieve this I added a EOL in the stringify as per below.

console.log("writing file");
        for(var i in allDataOut)
        {
            fs.appendFileSync(outputFile, JSON.stringify(allDataOut[i]) + '\r\n', encoding='utf8');

        }    
        console.log("done");
Sign up to request clarification or add additional context in comments.

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.