Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON.parse() not failing when it should fail

I have the following NodeJS code:

const msg = Buffer.from([ 34, 115, 106, 100, 104, 34 ]);
const a = JSON.parse(msg.toString('utf-8'))
console.log(a);

The msg buffer is basically the string "sjdh" when using .toString(‘utf-8’) on it.

How come when I run this, JSON.parse is able to parse what is essentially the string "sjdh" and returns it as a result to a?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Some types are allowed to not be wrapped in an object or array by JSON.parse

Read more here What is the minimum valid JSON?

console.log(JSON.parse('"asdad"'))
console.log(JSON.parse('null'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading