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
Deserialize and Serialize in Single line - Winter 12
Visualforce Page Output as JSON
Google Map API - Demo

1
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
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.
• JSON Simple to represent

5
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
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
Nested Object JSON
{
“FirstName” : “Foo”,
“LastName” : “Bar”,
“Address” :
{
“city” : “Pune”,
“state” : “Maharashtra”,
“country” : “India”
}

}
8
How to Work with JSON – Before
Winter 12
• AppExchange and other codes were available for
Parsing JSON.
• Performance Overhead.
• Learning curve more.

9
Deserialize JSON – Winter 12 API
JSON String :
{
“FirstName” : “Foo”,
“LastName” : “Bar”
}

Equivalent Apex Class:
Class Person
{
public String FirstName;
public String LastName;
}

10
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
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"
standardStylesheets="false"
sidebar="false"

14
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
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
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;
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

JSON Support in Salesforce - winter 12

  • 1.
    Salesforce JSON support Winter12 By – Jitendra Zaa 0
  • 2.
    Agenda • • • • • • • What is JSON Advantageof 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.
    What is JSON • • • • • JSONStands 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.
  • 5.
    JSON Example { “FirstName” :“Foo”, “LastName” : “Bar” } 4
  • 6.
    JSON Advantage overXML • XML is Verbose. • More size than of equivalent JSON representation because of repeated Tags. • JSON Simple to represent 5
  • 7.
    Why JSON • RESTis 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.
    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.
    Nested Object JSON { “FirstName”: “Foo”, “LastName” : “Bar”, “Address” : { “city” : “Pune”, “state” : “Maharashtra”, “country” : “India” } } 8
  • 10.
    How to Workwith JSON – Before Winter 12 • AppExchange and other codes were available for Parsing JSON. • Performance Overhead. • Learning curve more. 9
  • 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.
    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.
    Serialize Apex Classto JSON JSON.serialize(object); Object to convert to JSON 12
  • 14.
    JSON MIME Type •application/json 13
  • 15.
    Visualforce Page Outputas JSON • • • • contentType="application/x-JavaScript; charset=utf-8" showHeader="false" standardStylesheets="false" sidebar="false" 14
  • 16.
    Demo Get distance betweentwo 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.
    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.
    Apex Class Equivalentto 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.
    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