How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely?
相关问题
- Is there a limit to how many levels you can nest i
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
- StackExchange API - Deserialize Date in JSON Respo
I'd like to mention that there are alternatives to the global JSON object.
JSON.parse
andJSON.stringify
are both synchronous, so if you want to deal with big objects you might want to check out some of the asynchronous JSON modules.Have a look: https://github.com/joyent/node/wiki/Modules#wiki-parsers-json
you can require .json files.
For example if you have a
config.json
file in the same directory as your source code file you would use:or (file extension can be omitted):
note that
require
is synchronous and only reads the file once, following calls return the result from cacheAlso note You should only use this for local files under your absolute control, as it potentially executes any code within the file.
You can use
JSON.parse()
.You should be able to use the
JSON
object on any ECMAScript 5 compatible JavaScript implementation. And V8, upon which Node.js is built is one of them.Parsing a string containing JSON data
Parsing a file containing JSON data
You'll have to do some file operations with
fs
module.Asynchronous version
Synchronous version
You wanna use
require
? Think again!You can sometimes use
require
:But, I do not recommend this for several reasons:
require
is synchronous. If you have a very big JSON file, it will choke your event loop. You really need to useJSON.parse
withfs.readFile
.require
will read the file only once. Subsequent calls torequire
for the same file will return a cached copy. Not a good idea if you want to read a.json
file that is continuously updated. You could use a hack. But at this point, it's easier to simply usefs
..json
extension,require
will not treat the contents of the file as JSON.Seriously! Use
JSON.parse
.load-json-file
moduleIf you are reading large number of
.json
files, (and if you are extremely lazy), it becomes annoying to write boilerplate code every time. You can save some characters by using theload-json-file
module.Asynchronous version
Synchronous version
Parsing JSON from streams
If the JSON content is streamed over the network, you need to use a streaming JSON parser. Otherwise it will tie up your processor and choke your event loop until JSON content is fully streamed.
There are plenty of packages available in NPM for this. Choose what's best for you.
Error Handling/Security
If you are unsure if whatever that is passed to
JSON.parse()
is valid JSON, make sure to enclose the call toJSON.parse()
inside atry/catch
block. A user provided JSON string could crash your application, and could even lead to security holes. Make sure error handling is done if you parse externally-provided JSON.That's all.
This had to be shouted at me: it only works for
.json
files.If the file ending is different this does not work!
Use this to be on the safe side
var data = JSON.parse(Buffer.concat(arr).toString());