Parse Unconventional JSON Safely
Parsing JSON in JavaScript is straightforward using the built-in JSON.parse function. However, when dealing with "relaxed" JSON, where key names lack quotation marks, an error occurs.
This quandary arises when conducting JSON-based communication in development environments or automating test scripts. While eval offers a quick solution, it poses security concerns.
Escaping the Eval Pitfall
An alternative to eval while still allowing relaxed syntax involves sanitizing the JSON beforehand. By utilizing regular expressions, key names can be enclosed in double quotes, conforming to proper JSON structure. This approach allows us to safely parse unconventional JSON without compromising security.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3