Factor section of code in apiDelegate.

Fixing behavior to detect JSON string motivated the refactoring.

Also, update JavaScript coding style: " instead of ' to quote strings
and spacing within parentheses.