Skip to content

Instantly share code, notes, and snippets.

@TayKangSheng
Last active March 5, 2020 07:11
Show Gist options
  • Save TayKangSheng/7a26038a2145dc61e29bcc1069f04352 to your computer and use it in GitHub Desktop.
Save TayKangSheng/7a26038a2145dc61e29bcc1069f04352 to your computer and use it in GitHub Desktop.
Converting JSON to JavasScript Object
/*
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