SHARE
TWEET

Untitled

a guest Apr 19th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Describe JSON and explain what it is used for.
  2. #### Introduction to JSON  
  3.  
  4. 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.
  5.  
  6. 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.
  7.  
  8. #### Syntax Rules
  9.  
  10. * JSON requires starting and ending braces.
  11. * Name/Value pairs must be separated by commas.
  12. * JSON requires double quotation marks around name/value pairs.
  13.  
  14. For example:
  15.  
  16. ```JavaScript
  17. {
  18.   "fruit" : "banana",
  19.   "vegetable" : "carrot"
  20. }
  21.  
  22. ```
  23.  
  24. * 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.
  25.  
  26. For example:
  27.  
  28. ```javascript
  29. // Will throw a SyntaxError
  30. {
  31.     "ticketNo" : 0100
  32. }
  33.  
  34. // Wrap the value in quotes
  35. {
  36.     "ticketNo" : "0100"
  37. }
  38. ```
  39.  
  40. ### JSON Methods
  41.  
  42. There are two JSON methods:  `JSON.stringify()` and `JSON.parse()`.
  43.  
  44. * `JSON.stringify` accepts a JavaScript object or value and returns a JSON string.
  45.  
  46.  
  47. ```javascript
  48. var obj = {fruit : "apple"};
  49. JSON.stringify(obj);
  50.  
  51. // "{"fruit" : "apple"}"
  52. ```
  53.  
  54. * `JSON.parse` parses a JSON string and returns a JavaScript object or value.
  55.  
  56. ```javascript
  57. var str = '{"fruit" : "apple"}';
  58. JSON.parse(str);
  59.  
  60. // {fruit : "apple"}
  61. ```
  62.  
  63. ### Usage
  64.  
  65. 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.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top