Google Local's AJAX Data Objects Demystified
Background
JSON standard requires the name in the name value pair to be a string enclosed in quotation marks. The Google Local server sends AJAX data object in a JSON-like format in which the name is not enclosed in quotation marks. For example:
title: "from: ... to: 15939 ... - Google Maps",
vartitle: "",
url: "/maps?saddr=...&daddr=...&ie=UTF8",
urlViewport: false,
form:
{
selected: "d",
q:
{
q: "from: ... to: ..."
},
.
.
.
}
This disparity between actual data object and the standard can easily be eliminated by enclosing all the names in quotes. The best way to achieve this is to use regular expression. A regular expression can easily find a name by searching for a alphanumeric word before colon. Of course, care must be taken not to confuse words before colons within a string value. The following code snippet shows how to achieve this:
false = False
true = True
null = None
# Helper RegEx based function to enclose names in quotes
# (which is required in JSON) - Google server does not put
# quote around names
# E.g. {title: "From: Home To: Office"} is converted into
# {"title": "From: Home To: Office"}
import re
pat = re.compile(r"""
(?P<name>\w+) # Name of JSON pair
\s* : # Whitespace, and a colon
| # Or
(".*?") # Enclosed in double quotes
""", re.VERBOSE)
def subfunc(match):
# If pattern was found enclosed in double quotes,
# do not substitute
if match.group(2):
return match.group(2)
else:
return '"'+match.group(1)+'"'+':'
This "pre-processed" data object is ready to be consumed by our JSON parser to give us all the information we need from the Google Local server.
Summary
This little log just goes on to show that with Python (even on an S60 handset) we can get the information we need in the format we want with minimal effort. I wasted much time thinking about parsing techniques before I stumbled across this simple solution. And there is unmistakable elegance in simplicity, don't you think?
Labels: AJAX, Google Local, JSON, Python, S60