Function
$().SPServices.SPXmlToJson
Certification
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 byGetListItems. 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 |
*Both the FileRef and FileDirRef have Type="Lookup". I have special cases in the code to parse those two columns out appropriately.
mapping: {
ows_FileLeafRef: { mappedName: "Name", objectType: "Lookup" },
ows_FileRef: { mappedName: "Url", objectType: "Lookup" }
}
If you would like just the text back so that you can parse it yourself, then stick with objectType: "Text" and parse it using .split()
myFileRef = data[i].Url.split(";#")[1];
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: