Sage CRM's RESTful API: SData (Part 9 of 10)

3 minute read time.

We have seen previously in this series that we can make a request directed at an instance of Sage CRM using SData 2.0 to fetch some data from an entity.

  • http://localhost/sdata/crmj/sagecrm2/-/company('28')

The result received when we use SData 2.0 is in JSON format.

JSON stands for JavaScript Object Notation. JSON is an open standard format that uses human-readable text to transmit data objects consisting of attribute—value pairs. This is means that it is just plain text that is transmitted with data that is arranged not using XML tags but rather it uses a layout that is the same as the way in which an Object can be described in JavaScript. It is used primarily to transmit data between a server and web application, as an alternative to XML.

Data formatted according to the JSON standard is lightweight and can be parsed by JavaScript implementations very easily. This make it an ideal data exchange format for web (and mobile) applications and of course for Sage CRM.

The information transported through JSON is always in form of the following basic types

  • Strings
  • Numbers
  • Booleans
  • null

The strings are double-quoted Unicode (UTF-8 by default), with backslash escaping.

JSON describes objects and uses names to access its "members" which are desribed as a 'tuple' or a name/value pair.

For example

{"Conf_name": "Sage Insights ","Conf_EndDate":"2015-02-07T00:00:00-05:00"}

We can use this within code to build an object in Javascript.

var myobj = {"Conf_name": "Sage Insights ", "Conf_EndDate":"2015-02-07T00:00:00-05:00"}
alert(myobj.conf_name);

In this example, myobj.conf_name returns Sage Insights:

Part of the nature of JavaScript is that all the properties or members of the object are 'unordered' and they are identified by name.

If you need ordered sets of data within a JSON object the arrays can be used.

E.g.

{"Conf_delegates": ["Partner","Customer"]}

An ordered set of contents is delimited by square brackets ("[" "]").

Example: "addr_country" : [ "Albania" , "Belgium", "Brazil", "Dominica", "Equador", "France" ]

These are defined as an array when used to create a JavaScript object.

An JSON allows sub-objects to be passed

{"Conf_name": "Sage Insights ", "Conf_EndDate":"2015-02-07T00:00:00-05:00","Conf_CreatedBy":{"$value":1,"$title":"Jeff Richards"}
}

The sub object in the example above describes the value and the display text of a field. These JSON objects are also unordered sets of name-value pairs. The only restriction is that within a set the names are unique. The name-value pairs like in the parent object definition are separated by a comma (",") and enclosed by curly brackets ("{" "}")

When it comes to working with JSON with the RESTful API then the results are going to be of particular types. The response types are

  • Entry
  • Feed
  • Diagnosis

Example Entry

Request

  • http://localhost/sdata/crmj/sagecrm2/-/company('28')

The result describes a single record as a JSON object.

Example Feed

Request

  • http://localhost/sdata/crmj/sagecrm2/-/company

The result describes a list or set of objects in JSON format. The list of these objects are then within the $resources property of the return object.

Example Diagnosis

When an illegal or erroneous request is made the result that is returned is in the format of a Diagnosis message.

Request:

  • http://localhost/sdata/crmj/sagecrm2/-/coxxxxmpany

Here you can see that a spelling mistake has been made in the name of the table to which the request is directed.

The result is the Diagnosis message.

So far though we have only looked at issuing simple fetches of data. The last article in this series will look at how SData 2.0 allows us to perform full CRUD operations using the RESTful API.