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
Just want to complete the answer (as I struggled with it for a while), want to show how to access the json information, this example shows accessing Json Array:
My solution:
Another example of JSON.parse :
If you want to add some comments in your JSON and allow trailing commas you might want use below implemention:
Note that it might not work well if you have something like
"abc": "foo // bar"
in your JSON. So YMMV.JSON.parse will not ensure safety of json string you are parsing. You should look at a library like json-safe-parse or a similar library.
From json-safe-parse npm page:
Just to make this as complicated as possible, and bring in as many packages as possible...
This lets you do:
Or if you're using async/await:
The advantage over just using
readFileSync
is that your Node server can process other requests while the file is being read off disk.