How can you stream a JSON file in Node.js and interpret it as JSON? -
i have json file want use in node.js script. want pass json , script parse it. tried using fs.readfilesync(filename)
, returns buffer. how can convert json can parse it?
if read buffer, can convert json.parse()
.
> var fs = require('fs'); undefined > a=fs.readfilesync('a.json'); <buffer 7b 0d 0a 20 20 20 20 22 67 6c 6f 73 73 61 72 79 22 ...> > json.parse(a); { glossary: { title: 'example glossary', glossdiv: { title: 's', glosslist: [object] } } }
file must valid json.
you can require
directly load json.
> var a=require('a.json'); undefined > { glossary: { title: 'example glossary', glossdiv: { title: 's', glosslist: [object] } } }
by default require checks inside node_modules
folder inside current directory not current folder. should give path.
Comments
Post a Comment