Creating .json file and storing data in it with JavaScript?
1- make your obj:
var dict = {"one" : [15, 4.5],
"two" : [34, 3.3],
"three" : [67, 5.0],
"four" : [32, 4.1]};
2- make it JSON:
var dictstring = JSON.stringify(dict);
3- save your json file and dont forget that fs.writeFile(...)
requires a third (or fourth) parameter which is a callback function to be invoked when the operation completes.
var fs = require('fs');
fs.writeFile("thing.json", dictstring, function(err, result) {
if(err) console.log('error', err);
});
Simple! You can convert it to a JSON (as a string).
var dictstring = JSON.stringify(dict);
To save a file in NodeJS:
var fs = require('fs');
fs.writeFile("thing.json", dictstring);
Also, objects in javascript use colons, not equals:
var dict = {"one" : [15, 4.5],
"two" : [34, 3.3],
"three" : [67, 5.0],
"four" : [32, 4.1]};