I Pounce On The Pownce API

The microblogging site http://www.pownce.com/ has opened to the general public so I decided to give their API a try. But the main point of this blog post is to dispute the assertion that JSON is "easy for humans to read and write". It is not easy to read! JSON is a mess of curly braces, brackets, and quoted strings that is even more difficult to read than XML. I had considerable trouble working with the JSON data that Pownce provides because it seems to be nonstandard and requires the use of array indices which aren't usually needed. At first I tried to determine the data's object model by loading the XML version into XML Spy and finding the XPaths for a value. This was only slightly useful. Then I found a JSON Viewer on CodePlex which proved to be extremely helpful.

 As you can see in the screen capture below, the JSON Viewer clearly indicates that to reference a profile image you need to use the following syntax:

obj.notes[0].note[11].sender[5].profile_photo_urls[0].tiny_photo_url

JSON Viewer 

http://pownce.pbwiki.com/API+Documentation1-1 Pownce provides a JSON data feed with a callback function so there was no need for my XML2JSON generic handler.

Pownce Public Notes List

// this function must appear before the remote script block, requires jQuery and jquery.flydom
function ws_results(obj) {
//    alert(obj.notes.length);
//    for (prop in obj.notes[0].note[0]) {
//		alert(prop);
//	}
     $('#response').createAppend(
            'table', { width: '500px', className: 'tableData', id: 'Notes' }, [
                'tr', { }, [
                    'th', { align: 'center' }, 'Pownce Public Note List'
                           ]
               ,'tr', { }, [
                'td', { align: 'left', id: 'info', style: 'font-size: 10pt;' }
                           ]             
            ]
        );
        // loop through items
        for (var i = 0; i < obj.notes.length; i++) {
            // create table row
            $('#Notes').createAppend(
                'tr', { }, [
                'td', { align: 'left', id: 'note' + i, style: 'font-size: 10pt' } 
                          ] 
            );
              $('#note' + i).createAppend('b', { }, 'ID: ');
              $('#note' + i).append(obj.notes[i].note[0].id);
              $('#note' + i).createAppend('br', { });
              $('#note' + i).createAppend('b', { }, 'Permalink: ');
              $('#note' + i).createAppend('a', { href: obj.notes[i].note[1].permalink }, obj.notes[i].note[1].permalink);
              $('#note' + i).createAppend('br', { });
              $('#note' + i).createAppend('b', { }, 'Type: ');
              $('#note' + i).append(obj.notes[i].note[2].type);
              $('#note' + i).createAppend('br', { });
              if (obj.notes[i].note[2].type == "link") {
                  $('#note' + i).createAppend('b', { }, 'Link: ');
                  $('#note' + i).createAppend('a', { href: obj.notes[i].note[3].link[0].url }, obj.notes[i].note[3].link[0].url);
                  $('#note' + i).createAppend('br', { });
              }
              $('#note' + i).createAppend('b', { }, 'Body: ');
              if (obj.notes[i].note[2].type == "message") {
                  $('#note' + i).append(obj.notes[i].note[3].body)
              }
              else {
                  $('#note' + i).append(obj.notes[i].note[4].body);  
              }
              $('#note' + i).createAppend('br', { });
              $('#note' + i).createAppend('b', { }, 'Timestamp: ');
              if (obj.notes[i].note[2].type == "message") {
                  $('#note' + i).append(obj.notes[i].note[4].timestamp);
              }
              else {
                  $('#note' + i).append(obj.notes[i].note[5].timestamp);
              }              
              $('#note' + i).createAppend('br', { });
              $('#note' + i).createAppend('b', { }, 'Display Since: ');
              if (obj.notes[i].note[2].type == "message") {
                  $('#note' + i).append(obj.notes[i].note[6].display_since);
              }
              else {
                  $('#note' + i).append(obj.notes[i].note[7].display_since);
              }               
              $('#note' + i).createAppend('br', { });
              $('#note' + i).createAppend('b', { }, 'Sender: ');
              if (typeof(obj.notes[i].note[10].sender) == "undefined") {
				  $('#note' + i).append(obj.notes[i].note[11].sender[0].username);             
              }
              else {
                  $('#note' + i).append(obj.notes[i].note[10].sender[0].username);               
              }
              $('#note' + i).createAppend('br', { });
              $('#note' + i).createAppend('b', { }, 'Photo: ');
              if (typeof(obj.notes[i].note[10].sender) == "undefined") {
                  $('#note' + i).createAppend('img', { src: obj.notes[i].note[11].sender[5].profile_photo_urls[3].medium_photo_url, align: 'left', hspace: '5'}, null);            
              }
              else {
                  $('#note' + i).createAppend('img', { src: obj.notes[i].note[10].sender[5].profile_photo_urls[3].medium_photo_url, align: 'left', hspace: '5'}, null);              
              }
              $('#note' + i).createAppend('br', { });              
        }
}

No Comments