Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  166] [ 1]  / answers: 1 / hits: 113511  / 10 Years ago, sun, august 31, 2014, 12:00:00

I have a back-end JavaScript file that runs on node.js. It do some stuff using async.series and yields the final dictionary(object) with data I need on my front-end. I now how to read a .json file and convert it into the JavaScript object, but I do not know create .json file with back-end JavaScript and how to store some data in it.



Could anyone please tell me the right way to do this.



Here is the dictionary(object) that I need to convert and store to the .json file.



var dict = {one : [15, 4.5],
two : [34, 3.3],
three : [67, 5.0],
four : [32, 4.1]};

More From » json

 Answers
27

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]};

[#69604] Wednesday, August 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jalyn

Total Points: 173
Total Questions: 96
Total Answers: 90

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;