Last active
March 5, 2020 07:11
-
-
Save TayKangSheng/7a26038a2145dc61e29bcc1069f04352 to your computer and use it in GitHub Desktop.
Converting JSON to JavasScript Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Converting JSON file | |
{ | |
"one": "1", | |
"two": "2" | |
} | |
to JavaScript Object | |
{ | |
one: "1", | |
two: "2" | |
} | |
*/ | |
let fs = require('fs'); | |
let data = require("./data.json"); | |
let util = require('util'); // https://nodejs.org/en/knowledge/getting-started/how-to-use-util-inspect/ | |
string_data = JSON.stringify(data); | |
parsed_data = JSON.parse(string_data); | |
fs.writeFile('./countries_af.ts', util.inspect(parsed_data, { compact: true, showHidden: false, depth: null, maxArrayLength: null }), (err) => { | |
// throws an error, you could also catch it here | |
if (err) throw err; | |
// success case, the file was saved | |
console.log('Data Written'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment