Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Describe JSON and explain what it is used for.
- #### Introduction to JSON
- JSON stands for JavaScript Object Notation and it is a language-independent format used for the transmission of data across a network. JSON is lightweight and compact, making the transfer of data between systems faster due to less markup overhead.
- While the JSON syntax may look like a JavaScript object, when used in a string context, the value `'{"prop": "value"}'` is a JSON string. Without surrounding quotes, it qualifies as an object literal.
- #### Syntax Rules
- * JSON requires starting and ending braces.
- * Name/Value pairs must be separated by commas.
- * JSON requires double quotation marks around name/value pairs.
- For example:
- ```JavaScript
- {
- "fruit" : "banana",
- "vegetable" : "carrot"
- }
- ```
- * Leading zeros are not allowed. The first example below will throw a SyntaxError exception if the string is not valid JSON. To keep a leading zero, wrap the number inside quotes.
- For example:
- ```javascript
- // Will throw a SyntaxError
- {
- "ticketNo" : 0100
- }
- // Wrap the value in quotes
- {
- "ticketNo" : "0100"
- }
- ```
- ### JSON Methods
- There are two JSON methods: `JSON.stringify()` and `JSON.parse()`.
- * `JSON.stringify` accepts a JavaScript object or value and returns a JSON string.
- ```javascript
- var obj = {fruit : "apple"};
- JSON.stringify(obj);
- // "{"fruit" : "apple"}"
- ```
- * `JSON.parse` parses a JSON string and returns a JavaScript object or value.
- ```javascript
- var str = '{"fruit" : "apple"}';
- JSON.parse(str);
- // {fruit : "apple"}
- ```
- ### Usage
- Because JSON is minimal and easier to read than XML, it is a popular format for transmitting data between a server and an application. APIs that return large data sets often serialize data in JSON. These JSON strings can then be quickly parsed by machines and still be readable by humans.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement