AWG Blogs

Tuesday, March 26, 2013

JSONSelect Usage

For the simple use case of verifying the existence of a "record" in a JavaScript object, you typically need to iterate through all items. There is a one-liner approach though, thankfully.

Say you want to verify that an ID exists in
 "Body": {
  "GetListCollectionResponse": {
   "GetListCollectionResult": {
    "Lists": {
     "List": [
      {
       "ID": "{B8B000CC-EB90-4B3D-9A44-79B8A9CFCAAC}",
       "RequireCheckout": "False",
       "EnableMinorVersion": "False",
...
Verify existence with the following function:
ListExists = function(listGuid) { //search spsvcListColXmlJson for match
  var arr = JSONSelect.match(":has(:root > .ID:val(\""+listGuid+"\"))", 
   spsvcListColXmlJson.Body.GetListCollectionResponse.GetListCollectionResult.Lists);
  var retval = arr.length>0;
  return retval; 
 };
Test:
test('List Guid found in Lists collection', function() {
 ok(ListExists("{B8B000CC-EB90-4B3D-9A44-79B8A9CFCAAC}"),"list should exist");
});