Salesforce JSON support
Winter 12

By – Jitendra Zaa

0
Agenda
•
•
•
•
•
•
•

What is JSON
Advantage of JSON over XML
Other reasons to use JSON
JSON Syntax / Guidelines
Deseriali...
What is JSON
•
•
•
•
•

JSON Stands for “JavaScript Object Notation”.
Language Independent representation of objects.
Pars...
XML Example
<Person>
<FirstName>Foo</FirstName>
<LastName>Bar</LastName>
</person>

3
JSON Example
{
“FirstName” : “Foo”,
“LastName” : “Bar”
}

4
JSON Advantage over XML
• XML is Verbose.
• More size than of equivalent JSON representation
because of repeated Tags.
• J...
Why JSON
• REST is very common and supports JSON widely.
• REST and JSON are the music of the internet.
• Google , Yahoo, ...
JSON Guidelines
Key – Value enclosed in double quotes (String).
Value - can again anything of datatype.
{} – represents Ob...
Nested Object JSON
{
“FirstName” : “Foo”,
“LastName” : “Bar”,
“Address” :
{
“city” : “Pune”,
“state” : “Maharashtra”,
“cou...
How to Work with JSON – Before
Winter 12
• AppExchange and other codes were available for
Parsing JSON.
• Performance Over...
Deserialize JSON – Winter 12 API
JSON String :
{
“FirstName” : “Foo”,
“LastName” : “Bar”
}

Equivalent Apex Class:
Class P...
Deserialize JSON – Single Line
Person d = (Person )System.JSON.deserialize(jsonString, Person.class);

JSON Format String
...
Serialize Apex Class to JSON
JSON.serialize(object);

Object to convert to
JSON

12
JSON MIME Type
• application/json

13
Visualforce Page Output as JSON
•
•
•
•

contentType="application/x-JavaScript; charset=utf-8"
showHeader="false"
standard...
Demo
Get distance between two points using Google API
http://maps.googleapis.com/maps/api/distancematrix/json?origi
ns=Nag...
Response from Google
{

}

destination_addresses:[
"Pune, Maharashtra, India"
],
origin_addresses:[
"Nagpur, Maharashtra, ...
Apex Class Equivalent to JSON
response
Class GoogleResponse
{
public String[] destination_addresses;
public String[] origi...
Deserialize JSON
String json = '{ "destination_addresses":[ "Pune, Maharashtra, India" ],
"origin_addresses":[ "Nagpur, Ma...
Upcoming SlideShare
Loading in …5
×

JSON Support in Salesforce - winter 12

3,185 views

Published on

What is JSON. What is use of JSON. Working with JSON in Salesforce

Published in: Technology
  • Be the first to comment

  • Be the first to like this

JSON Support in Salesforce - winter 12

  1. 1. Salesforce JSON support Winter 12 By – Jitendra Zaa 0
  2. 2. Agenda • • • • • • • What is JSON Advantage of JSON over XML Other reasons to use JSON JSON Syntax / Guidelines Deserialize and Serialize in Single line - Winter 12 Visualforce Page Output as JSON Google Map API - Demo 1
  3. 3. What is JSON • • • • • JSON Stands for “JavaScript Object Notation”. Language Independent representation of objects. Parsers widely available in many languages. Alternative to XML. JSON can be transmitted over http / https. 2
  4. 4. XML Example <Person> <FirstName>Foo</FirstName> <LastName>Bar</LastName> </person> 3
  5. 5. JSON Example { “FirstName” : “Foo”, “LastName” : “Bar” } 4
  6. 6. JSON Advantage over XML • XML is Verbose. • More size than of equivalent JSON representation because of repeated Tags. • JSON Simple to represent 5
  7. 7. Why JSON • REST is very common and supports JSON widely. • REST and JSON are the music of the internet. • Google , Yahoo, D&B web services exposed as REST with response type JSON. 6
  8. 8. JSON Guidelines Key – Value enclosed in double quotes (String). Value - can again anything of datatype. {} – represents Object [] – represents Array , - Separates data element within Object Supports basically four data type: 1. Boolean 2. Number 3. String 4. Object 7
  9. 9. Nested Object JSON { “FirstName” : “Foo”, “LastName” : “Bar”, “Address” : { “city” : “Pune”, “state” : “Maharashtra”, “country” : “India” } } 8
  10. 10. How to Work with JSON – Before Winter 12 • AppExchange and other codes were available for Parsing JSON. • Performance Overhead. • Learning curve more. 9
  11. 11. Deserialize JSON – Winter 12 API JSON String : { “FirstName” : “Foo”, “LastName” : “Bar” } Equivalent Apex Class: Class Person { public String FirstName; public String LastName; } 10
  12. 12. Deserialize JSON – Single Line Person d = (Person )System.JSON.deserialize(jsonString, Person.class); JSON Format String Determine the output object type of the JSON 11
  13. 13. Serialize Apex Class to JSON JSON.serialize(object); Object to convert to JSON 12
  14. 14. JSON MIME Type • application/json 13
  15. 15. Visualforce Page Output as JSON • • • • contentType="application/x-JavaScript; charset=utf-8" showHeader="false" standardStylesheets="false" sidebar="false" 14
  16. 16. Demo Get distance between two points using Google API http://maps.googleapis.com/maps/api/distancematrix/json?origi ns=Nagpur+Maharashtra+india&destinations=Pune+Maharasht ra+india&sensor=false 15
  17. 17. Response from Google { } destination_addresses:[ "Pune, Maharashtra, India" ], origin_addresses:[ "Nagpur, Maharashtra, India" ], rows:[ { elements:[ { distance:{ text:"730 km", value:730315 }, duration:{ text:"11 hours 25 mins", value:41126 }, status:"OK" } ] } ], status:"OK" 16
  18. 18. Apex Class Equivalent to JSON response Class GoogleResponse { public String[] destination_addresses; public String[] origin_addresses; public Row[] rows; public String status; } Class Row { public Elements[] elements; } Class Elements { public public public } Class Values { public public } 17 Values distance; Values duration; String status; String text; String value;
  19. 19. Deserialize JSON String json = '{ "destination_addresses":[ "Pune, Maharashtra, India" ], "origin_addresses":[ "Nagpur, Maharashtra, India" ],"rows":[ { "elements":[ { "distance":{ "text":"730 km", "value":730315 }, "duration":{ "text":"11 hours 25 mins", "value":41126 }, "status":"OK" } ] } ], "status":"OK"}'; GoogleResponse d = (GoogleResponse)System.JSON.deserialize(json, GoogleResponse.class); 18

×