Reputation: 6043
There is a website (http://xisbn.worldcat.org/xisbnadmin/doc/api.htm) that offers its API in several formats, including XML, CSV, JSON and something called "python". I am assuming this is related to the Python programming language, but how? The output looks like this:
{
'stat':'ok',
'list':[{
'url':['http://www.worldcat.org/oclc/177669176?referer=xid'],
'publisher':'O\'Reilly',
'form':['BA',
'DA'],
'lccn':['2004273129'],
'lang':'eng',
'city':'Sebastopol, CA',
'author':'by Mark Lutz and David Ascher.',
'ed':'2nd ed.',
'year':'2003',
'isbn':['0596002815'],
'title':'Learning Python',
'oclcnum':['177669176',
'222927677',
'249274099',
'253402825',
'301161087',
'438280230',
'442197411',
'464709193',
'492988633',
'54619668',
'55847258',
'614957020',
'644729085',
'760707144',
'772683553',
'802989466',
'850841661',
'851226517',
'875412584']}]}
This looks like JSON to me, but the JSON output, while similar, is this:
{
"stat":"ok",
"list":[{
"url":["http://www.worldcat.org/oclc/177669176?referer=xid"],
"publisher":"O'Reilly",
"form":["BA",
"DA"],
"lccn":["2004273129"],
"lang":"eng",
"city":"Sebastopol, CA",
"author":"by Mark Lutz and David Ascher.",
"ed":"2nd ed.",
"year":"2003",
"isbn":["0596002815"],
"title":"Learning Python",
"oclcnum":["177669176",
"222927677",
"249274099",
"253402825",
"301161087",
"438280230",
"442197411",
"464709193",
"492988633",
"54619668",
"55847258",
"614957020",
"644729085",
"760707144",
"772683553",
"802989466",
"850841661",
"851226517",
"875412584"]}]}
The "python" example doesn't parse as valid JSON, so what is it? How would one decode this information in Python?
Upvotes: 0
Views: 189
Reputation: 1123400
It is a Python literal; it uses Python syntax for everything. Python syntax for dictionaries and lists and strings is quite close to the equivalent JSON syntax. The Python strings are byte strings however, not Unicode values like they would be in JSON.
Any null
JSON values will be represented as None
in Python, and any booleans will be rendered as True
and False
rather than the JSON true
and false
.
You can parse it with the ast.literal_eval()
function, but this is going to be slower than parsing JSON. Don't ever be tempted to use eval()
on such data; all it takes is for the API DNS entry to be hijacked for an attacker to feed you arbitrary Python objects and compromise your process.
Frankly, offering a Python syntax is rather pointless, in my view. They offer PHP and Ruby variants too; all that varies are the delimiters around the types. This is, at best, a curiosity; presumably parsing JSON will be faster in those other languages too (as parsing Python, Ruby or PHP requires support for a lot more types), or opening those processes up for eval()
-style attacks.
Upvotes: 3