One of the most common use of JSON is to fetch JSON data from a web server (as a file or as an HttpRequest), convert the JSON data to a JavaScript object, and then it uses the data in a web page.
For simplicity, this can be demonstrated by using a string as input (instead of a file).
Create a JavaScript string containing JSON syntax:
Since JSON syntax is a subset of JavaScript syntax, the JavaScript function eval() can be used to convert a JSON text into a JavaScript object.
The eval() function uses the JavaScript compiler which will parse the JSON text and produce a JavaScript object. The text must be wrapped in parenthesis to avoid a syntax error:
Use the JavaScript object in your page:
The eval() function can compile and
execute any JavaScript. This represents a potential security problem.
It is safer to use a JSON parser to convert a JSON text to a JavaScript object. A JSON parser will recognize only JSON text and will not compile scripts.
In browsers that provide native JSON support, JSON parsers are also faster.
Native JSON support is included in newer browsers and in the newest ECMAScript (JavaScript) standard.
| Web Browsers Support | Web Software Support |
|---|---|
|
|
For older browsers, a JavaScript library is available at https://github.com/douglascrockford/JSON-js
The JSON format was originally specified by Douglas Crockford
Your message has been sent to W3Schools.