Moving from SPServices to REST, Part 3: JSON vs. XML

This entry is part 3 of 6 in the series Moving from SPServices to REST

Summary: When you move from SOAP to REST, you’ll want to understand the differences between XML and JSON as well. Modern Web developers are most likely to use JSON, and this is a great time to switch.

Most auto mechanics understand that there are several different measurement systems – and therefore toolsets – that they have to use to fix up cars, whether under the hood or on the outside body. Where I sit here in the USA, it’s our unique system versus almost everyone else’s (metric). That’s sort of where we are if we’re still using SOAP and XML instead of REST and JSON.

2014 Fiat 500

Under the hood of a 1956 T-Bird by Mark Turnauckas https://flic.kr/p/f14uV8

The SOAP Web Services speak XML. That is, we send XML in our requests and we get XML in the responses. These days many developers feel those services are “chatty” and that “XML is bloated.” I’m not so sure that’s really the case, but I can go along with it.

When we receive XML in the response from a SOAP call to the server, we have to parse it apart so that we can use the data. The most common operation that people use in SPServices is GetListItems, which allows us to pull list data from the server to the client. With a call to GetListItems we get a packet back that looks something like this from a Tasks list:

call-to-getlistitems-returns-task-list-2

With the SOAP Web Services, XML is our only option. There’s no way to request anything else. REST, on the other hand, is usually used to request JSON.

To bring SPServices more into the modern way of doing things, I added a function in 0.7.1 called SPXmlToJson and then another in 2014.01 called SPGetListItemsJson. These two functions make it easy – really easy – to convert XML to JSON. You don’t have to know much about either method of storing data to get JSON back.

With REST we can still choose to get XML in the response, but we also have the option to request that the server send us JSON. Because JSON is a text representation of JavaScript objects, the conversion is much more straightforward, with just a call to $.parseJSON (assuming you’re using jQuery).

JSON is preferable because it doesn’t require much – if any – translation when we want to use it. You may already be converting the XML you get back with SPServices calls into JSON, either manually or with a call to SPXmlToJson or SPGetListItemsJson.

JSON stands for JavaScript Object Notation. It’s a way to store data in a complex, object-based JavaScript structure rather than what many of you may be used to doing with individual variables.

For example, when I get a task back from GetListItems, I could create a set of variables, like this:

var thisId = $(this).attr("ows_ID");
var thisTitle = $(this).attr("ows_Title");
var thisDueDate = $(this).attr("ows_DueDate");

When I answer questions in the SPServices Discussions, a lot of the code people post does something like this. It’s the way I used to do things, too.

With a call to REST, I get an object back that is all ready to go.

call-to-rest-gives-object-back-3

Taking it one step further, I can build a constructor function that takes the task as I get it back from a call to the server and converts it into a JSON structure of my own choosing, perhaps adding a few new properties I may need down the road:

function Task(data) {
  this.ID = data.ID;
  this.Title = data.Title;
  this.DueDate = moment(data.DueDate);
  this.Status = data.Status;
  // etc.

  this.Complete = this.Status === "Completed" ? true : false;

};

Note the conversion of the DueDate in the function above. In most of my applications these days, I use the MomentJSJavaScript library. It gives us a huge number of date- and time-oriented functions. Dealing with date/time conversions and arithmetic can be extremely messy, and MomentJS makes it easy. So rather than simply storing the text-based value of DueDate that I get back in the XML or JSON from a call, I store the DueDate converted into a “moment,” which is the lingua franca to use MomentJS’s helpful functions.

In the code examples above, I’ve gone from the rather brute force method I used to use where I used a lot of individual variables, to using a JSON representation, to using a constructor function to “build” each task from the results of a call. The first two of these methods require basically the same amount of code, but the second is far easier to work with when we pass the task back out to whatever code we want to work with it next. The third approach, while it introduces yet another level of abstraction, gives us even more flexibility, as we can decide to add a new element to any tasks inside the constructor function and it will be available in tasks everywhere.

Using SPXmlToJson and SPGetListItemsJson is another way to move your code forward without actually converting to the REST services right away, say if you are still on SharePoint 2007 or 2010. In fact, one of the main reasons I wrote these two functions was so that I could start working with JSON in a project on SharePoint 2007 using KnockoutJS. Yes, you can use the tools and frameworks all the cool kids are using even on ancient platforms.

In Part 2, I outlined a way for you to think about abstracting your CRUD calls using SPServices so that you could slide REST underneath at a later point. By using the two XML to JSON capabilities in SPServices, you can also start returning JSON to your applications from those abstractions, taking yourself one step closer to REST.

There are a few caveats here. I didn’t try to match the JSON formats that the SharePoint 2013 REST services return when I built my functions, though the way I hand you the data are similar. Similar doesn’t mean the same, of course.

Here’s an example. When you request list items using SPGetListItemsJson and don’t provide any custom mapping of your own, SPUser fields, which are also known as Person or Group columns, are returned in a JSON object that looks like this:

request-list-items-using-spgetlistitemsjson-returns-json-object-4

If you make a REST call to SharePoint 2013, SPUser fields are returned in a JSON object that looks like this, assuming you have requested the Id and Title using a projection (more on this in a future article in the series):

spuser-fields-returned-in-json-object-5

As you can see, there are some naming and formatting differences. I return “userId” and the REST call returns “Id”, for instance. In my opinion those differences are manageable, but it will take some work when you switch to REST because your application code will expect things in the format that SPGetListItemsJson provides. This is another reason why constructor functions can be helpful: they give us a place to do the isomorphic mapping from one data representation to another.

The upshot here is that you can:

  1. Abstract your CRUD operations as in Part 2, and
  2. Return JSON to your application rather than XML

As before, your application shouldn’t care where the data lives, how it’s stored or how we get it; it should only care about processing data that has a consistent format.

If you follow these suggestions, your code might go from something like this inline request:

var p = $().SPServices({
  operation: "GetListItems",
  ListName: "Tasks",
  CAMLViewFields: "<ViewFields>" +
                "<FieldRef Name='Title'/>" +
                "<FieldRef Name='AssignedTo'/>" +
                "<FieldRef Name='RelatedItems'/>" +
                "<FieldRef Name='Priority'/>" +
                "<FieldRef Name='Status'/>" +
                "<FieldRef Name='DueDate'/>" +
                "<FieldRef Name='Author'/>" +
                "</ViewFields>"
});

To this abstracted request:

// Get all the Tasks data
ProjectName.Promises.Tasks = $().ProjectName.Tasks.readAll();

// Read items from the Tasks list
$.fn.ProjectName.Tasks.readAll = function(options) {

  var opt = $.extend({}, {}, $.fn. ProjectName.defaults, options);
  var p = $().SPServices({
    operation: "GetListItems",
    ListName: "Tasks",
    CAMLViewFields: "<ViewFields>" +
        "<FieldRef Name='Title'/>" +
        "<FieldRef Name='AssignedTo'/>" +
        "<FieldRef Name='RelatedItems'/>" +
        "<FieldRef Name='Priority'/>" +
        "<FieldRef Name='Status'/>" +
        "<FieldRef Name='DueDate'/>" +
        "<FieldRef Name='Author'/>" +
      "</ViewFields>"
  });

  return p;

}; // End $.fn. ProjectName.Tasks.readAll

To this abstracted JSON-based request:

// Get all the Tasks data
ProjectName.Promises.Tasks = $().ProjectName.Tasks.readAll();

// Read items from the Tasks list
$.fn.ProjectName.Tasks.readAll = function(options) {

  var opt = $.extend({}, {}, $.fn. ProjectName.defaults, options);
  var p = $().SPServices.SPGetListItemsJson({
        listName: opt.TasksList,
        CAMLViewFields: opt.TasksListViewFields,
        CAMLQuery: camlQuery,
        CAMLQueryOptions: opt.TasksListQueryOptions,
        mappingOverrides: opt.TasksListMappingOverrides,
        changeToken: opt.changeToken
  });

  return p;

}; // End $.fn. ProjectName.Tasks.readAll

“Hold on here,” you might say. “That’s more code than I was writing before!” Absolutely true. And if you have a little snippet in a Content Editor Web Part (CEWP) that just reads a few items from a list and that’s it, then this effort might not be worth it. However, if you are building Single Page Applications (SPAs) or even little “portlets” for use throughout your Site Collection(s), it may make total sense.

For instance, if you have a customized Tasks or Announcements list in every Team Site that you are reading from and writing to, then abstracting those calls and storing them centrally may be a great idea. I do this sort of thing all the time. I load my data access operations into the master page in the Site Collection and then use them reliably from subsite to subsite. If you’re working on a larger team – which I tend not to do – it will require coordination, but it will also give you the ability to parse out atomic programming tasks across your team. Maybe one person is really good at data access and another is really good at HTML and CSS. This approach lets you parse out the work based on everyone’s skills and interests.

But I digress. The goal of this series is to help you move from SOAP to REST over time, not reorganize the roles on your team. Hopefully you can see that taking a few of these steps now or when you begin your next project, you’ll be putting yourself in a better position for the long run. These approaches may not help with everything you are using SOAP to accomplish – the SOAP services still cover more surface area than CSOM or REST, though that is changing – but it will put your most frequently-used sections of code in a good spot when it is time to move to REST.

Those auto mechanics know that there isn’t a huge difference between using a metric spanner and an Imperial wrench (other than getting everyone to agree on the terminology). XML and JSON are just dialects or different systems that accomplish basically the same thing: wrapping data up in packets that can be deciphered easily on the other end.

This article was also published on IT Unity on April 17, 2015. Visit the post there to read additional comments.
Series Navigation<< Moving from SPServices to REST, Part 2: New Patterns for SPServices DevelopmentMoving from SPServices to REST, Part 4: Converting GetListItems to REST >>

Similar Posts

2 Comments

  1. Hi Marc,

    I want to retrive data from sharepoint 2013 list from shareoint 2010 site.

    I have one content editor webpart in sharepoint 2010 to retrieve data from sharepoint 2013 list using webservices or REST protocol. Please suggest.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.