JSON (stands for JavaScript Object Notation) is a lightweight text-based standard designed for data interchange. Its MIME type is “application/json”. It is based on the JavaScript language for representing objects but it is language independent. There is a parser for most of the programming languages.
JSON is built on two structures: a collection of name/value pairs (object) and a list of values (array).
These are the basic data types we can represent on JSON:
- number
- string
- boolean
- array (list of values enclosed in square brackets)
- object (collection of key:value pairs enclosed in curly braces)
- null
This is an example of a JSON string:
1
| {"id":1,"name": "Rolando","age": 29,"nicknames": ["Rol","Rolo"]} |
A way to interpret a JSON formatted string in JavaScript is by using the eval() function, which was designed to evaluate JavaScript expressions. The result will be a native JavaScript object. There are some unicode characters that are invalid in JavaScript that will require backslash escaping.
The problem using eval() is it’s subject to security vulnerabilities. If the data we are evaluating does not come from a trusted source, it might result in a JavaScript code injection attack.
That is why there are many web browsers that support native JSON encoding/decoding, removing this security issue. Some examples:
- Firefox 3.5+
- Internet Explorer 8
- Google Chrome
- Safari