Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Technology
  • Be the first to comment


  1. 1. JSON (JavaScript Object Notation) 1
  2. 2. What is JSON? Light weight data-interchange format. Subset of JavaScript. Language independent. Text based. Easy to parse . 2
  3. 3. Why JSON?JSON is recognized natively by JavaScript. No need for parsing anXML document to extract the data. 3
  4. 4. JSON is Not… JSON is not a document format. JSON is not a markup language. 4
  5. 5. Properties of JSON Human and machine readable format. No support for Unicode. Self-documenting format that describes structure and field names. Strict syntax and parsing requirements. Represents the most general computer science data structures likerecords, lists and trees. 5
  6. 6. Where can JSON be used? Can be used for transferring medium amounts of data. Used in Java Script and then rendered on HTML pages. AJAX has many applications for JSON. Use JSON for applications that are browser based. 6
  7. 7. JSON Data Types Strings -Control Characters Number-Integer, Real and Floating Boolean-True and False Null 7
  8. 8. JSON Data Types (Cont…)StringStrings must be delimited by the double quote characters.Example: ”Address”, "444 Colombia"BooleanIn JSON “True" and “False" are pre-defined keywords.Example: “Active":True 8
  9. 9. Data types (Cont…)NumberJSON includes positive integers and Negative integers.Example: “Total points” -123NullNull string, also known as an empty string, is a string of zero length.Example: “Email":null 9
  10. 10. Simple Example of a JSON File{ "menu": "File", "commands": [ { "title": "New“, "action":"CreateDoc” }, { "title": "Open", "action": "OpenDoc” }, { "title": "Close“, "action": "CloseDoc” } ] } 10
  11. 11. JSON ObjectJSON Object Notation JSON object is an unordered set of name/value pairs and orderedlist of values. Object begins with { (left brace) and ends with } (right brace). Each name is followed by (colon). Name/value pairs are separated by , (comma). 11
  12. 12. JSON Representation of an Object Var HomeAddress = { "Address" : “Srini", "City" : “Bangalore", "PostalCode" : 580006 }; 12
  13. 13. JSON Functions JSON_decode — Decodes a JSON string. JSON_encode — Returns the JSON representation of a value. JSON_last_error — Returns the last error occurred. 13
  14. 14. JSON DecodeDecode parses the strings as a JSON-syntax string and returns thegeneric JSON object representation.json_decode()mixed json_decode ( string $json , bool $assoc) Takes a JSON encoded string and converts it into a PHP value.$json The JSON string being decoded$assoc False ----Return the value as an object True ----Return the value as an associative array 14
  15. 15. JSON Decode Example<?php$json = {"foo-bar": 12345};$obj = json_decode($json);print $obj->{foo-bar}; // 12345?> 15
  16. 16. JSON EncodeJSON encode returns the JSON representation of a value.string json_encode ( mixed $value )Returns a string containing the JSON representation of $value.$value Value being encoded, Can be any type except a resource. Function only works with UTF-8 encoded data. 16
  17. 17. JSON Encode Example<?php$arr = array (a=>1,b=>2,c=>3,d=>4,e=>5);echo json_encode($arr);// Output {"a":1,"b":2,"c":3,"d":4,"e":5}$arr = array ( 1, 2, 3, 4, 5 );echo json_encode($arr);// Output [1,2,3,4,5]$arr[x] = 10;echo json_encode($arr);// Output {"0":1,"1":2,"2":3,"3":4,"4":5,"x":10}echo json_encode(54321);// Output 54321?> 17
  18. 18. JSON in JavaScriptJavaScript Object Notation is a lightweight data-interchangeformat that is completely language independent text formatwhich is fast and easy to understand. 18
  19. 19. Example - JSON in JavaScript<html><head><title> Object creation in JSON in JavaScript</title><script language="javascript" >var JSONObject ={"name" : "Amit“,“address" : "Bangalore“,"age" : 23,"phone" : "4565763",“MobileNo" : 981100092}; 19
  20. 20. Example - JSON in JavaScript (Cont…)document.write("<h2><font color=skyblue>Name</font>::“"</h2>");document.write("<h2><font color=skyblue>Address</font>::“+JSONObject.address+"</h2>");document.write("<h2><font color=skyblue>Age</font>::“+JSONObject.age+"</h2>");document.write("<h2><font color=skyblue>Phone No.</font>::“"</h2>");document.write("<h2><font color=skyblue>Mobile No.</font>::“+JSONObject.MobileNo+"</h2>");</script></head><body bgcolor="#cc4488"><h3>Example of object creation in JSON in JavaScript</h3></body></html> 20
  21. 21. AJAX with JSONBoth XML and JSON use structured approaches to mark up data. More and more web services are supporting JSON.ExampleYahoos various search services, travel planners, and highwaytraffic services. 21
  22. 22. Why JSON is Better than Ajax?JSON is widely used in AJAX. The X in AJAX stands for XML.Example{"fullname": "Swati Kumar“, "org": "Columbia",}<?xml version=1.0‘ encoding=UTF-8?><element><fullname>Swati Kumar</fullname><org>Columbia</org></element> 22
  23. 23. JSON and XMLBenefits of JSON The easiness of readingThe easiness of usingBenefits of XMLXML is extensible.Widely used and recognized by almost all programming languages.Unfortunally, both XML and JSON are enable to integrate a largeamount of data in binary form. 23
  24. 24. Example{ "firstName": “Sahana", "lastName": "Saniya", "age": 25, "address": { "streetAddress": “2nd Street", "city": “Bangalore", "state": “India", }, "phoneNumber": [ { "type": "home", "number": “234243" }, { "type": "fax", "number": “645654" } ], "newSubscription": false, "companyName": null } 24
  25. 25. Equivalent for the above in XML could be<Person><firstName>John</firstName><lastName>Smith</lastName><age>25</age><address><streetAddress>21 2nd Street</streetAddress><city>New York</city><state>NY</state><postalCode>10021</postalCode></address><phoneNumber type="home">212 555-1234</phoneNumber><phoneNumber type="fax">646 555-4567</phoneNumber><newSubscription>false</newSubscription><companyName /></Person> 25
  26. 26. JSON is Like XML because… Both are self-describing and thus human readable. Both are hierarchical (i.e. we can have values within values.) Both can be parsed and used by lots of programming languages. Both can be passed around using AJAX (i.e. httpWebRequest). 26
  27. 27. JSON Vs XML JSON stands for JavaScript Object Notation, Considered as thesubset of JavaScript. XML on the other hand is a mark-up language that could be used indifferent languages. Parsing JSON encoded data is much faster than parsing XMLencoded data. 27
  28. 28. JSON Vs XML (Cont…)XML<result><people><person firstName=“Aman" lastName=“Valya"/><person firstName=“Suman" lastName=“Valya"/>...</people></result>JSON{people:[{ firstName: ‘Aman, lastName, ‘Valya },{ firstName: ‘Suman, lastName, ‘Valya },... 28
  29. 29. Why Use JSON Over XML? Lighter and faster than XML as on-the-wire data. JSON objects are typed while XML data is type less. JSON types are string, number, array, Boolean. XML data are all string. Data is readily accessible as JSON objects in your JavaScript Code. XML data needed to be parsed and assigned to variables throughtedious DOM APIs. 29
  30. 30. Features of JSON Provides multiple functionalities such as encode, decode/parse. Flexible, simple and easy to use. Supports streaming output of JSON text. Heap based parser. High performance. No dependency on external libraries. 30
  31. 31. JSON Data Interchange JSON is a simple, common representation of data. Communication between servers and browser clients. Communication between peers. Language independent data interchange. 31
  32. 32. Thank you 32