Function

$().SPServices.SPXmlToJson

Certification

Certified for SharePoint 2007 Certified for SharePoint 2010

Functionality

SPXslToJson is a function to convert XML data into JSON for client-side processing.

Syntax

$(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({ 
  mapping: {},
includeAllAttrs: false,
removeOws: true });

SPXmlToJson operates on a nodeset from XML returned by a SharePoint Web Services operation. In the current implementation, the nodeset must be "flat", as in the z:row elements retured by GetListItems. In other words, the function doesn't handle any nesting yet.

mapping
An array of columns to return in the JSON. While you should generally limit the values you request in your Web Services calls where you can, in some cases you won't have that capability. This option alows you to create "lean" JSON by only including the attributes you need. You can also rename the attributes for better compatibility with other jQuery plugins, for instance. Where it makes sense, the different column types (SPFieldType) are returned as analogous JavaScript objects. If not specified in the mapping, values are returned as strings.

The default value for mapping is {} (no mappings).

Currently supported objectTypes (SPFieldTypes):

SPFieldType (Type) JavaScript Object Type
Counter int
Integer int
DateTime Date()
User user = {userId, userName}
UserMulti Array of User
Lookup lookup = {lookupId, lookupValue}
LookupMulti Array of Lookup
Boolean true / false
MultiChoice Array of strings
Currency float

includeAllAttrs
If true, return all attributes, regardless whether they are in the mapping. The default is false.

removeOws
Specifically for GetListItems, if true, the leading "ows_" will be stripped from the field name.

Examples

This is the simplest example for using SPComplexToSimpleDropdown. You simply provide the nodeset:

$(xData.responseXML).SPFilterNode("z:row").SPXmlToJson();

In most cases, you'll pass in options which determine the structure of the JSON. Here is an example where I have provided mappings for a number of columns and I'm also including all of the attributes.

var myJson = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
   mapping: {
     ows_ID: {mappedName: "ID", objectType: "Counter"},
     ows_Title: {mappedName: "Title", objectType: "Text"},
     ows_Created: {mappedName: "Created", objectType: "DateTime"},
     ows_Author: {mappedName: "Author", objectType: "User"},
     ows_Country: {mappedName: "Country", objectType: "Lookup"},
     ows_City: {mappedName: "City", objectType: "LookupMulti"},
     ows_Lead_x0020_Source: {mappedName: "LeadSource", objectType: "MultiChoice"},
     ows_Sales_x0020_Rep: {mappedName: "SalesRep", objectType: "UserMulti"},
     ows_Potential_x0020_Value: {mappedName: "PotentialValue", objectType: "Currency"}
    },   // name, mappedName, objectType
   includeAllAttrs: true
  });

The results of the call above look like this, based on my test Sales Opportunities list:

image

Last edited Mar 20 at 4:59 PM by sympmarc, version 12

Comments

sympmarc Apr 13, 2012 at 3:53 PM 
The screenshot above shows what the JSON looks like in Firebug. You should be seeing the Title as a text string and the ID as a number, just as shown above.

Please use the Discussions rather than commenting on the doc pages, as sometimes I totally miss the questions.

M.

luciano_arias Apr 12, 2012 at 8:06 PM 
Hi Marc, this function seems pretty useful. However I didn't get what you show in the results picture.
All I got from the myJson var are [object] elements, see my snippet below. I am only testing with ID and Title from a simple list. If I go and drill down each object array then I get the value...is this how is supposed to work? (I am using jquery 1.7 and sp 0.7.1a .... Thanks

var myJson = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
mapping: {
ows_ID: {mappedName: "ID", objectType: "Counter"},
ows_Title: {mappedName: "Title", objectType: "Text"}
}, // name, mappedName, objectType
includeAllAttrs: false,
removeOws: true
});
alert(myJson) //-> return [object] array
alert(myJson[0]["ID"]); //returns the value