converting JSON to string

Adonis Vargas adonisv at REMOVETHISearthlink.net
Fri Jan 11 20:21:03 EST 2008


Gowri wrote:
> Hello,
> 
> I actually have two questions:
> 1. Are there any libraries which convert XML to JSON?
> 2. I am currently doing the above using the DOM parser and creating a
> JSON array
> 
> <snippet>
> for node in doc.getElementsByTagName("book"):
>                 isbn = node.getAttribute("isbn")
>                 titleNode = (node.getElementsByTagName("title")
> [0]).childNodes[0]
>                 title = titleNode.data
>                 primarykeys.append({'isbn': isbn, 'title': title})
> return primarykeys
> 
> I want to send primarykeys as a response to my client. i use
> mod_python and apache. The problem is, I have not been able to figure
> out how to convert my JSON output to a string.
> 
> Could someone please help me?
> 
> Thanks in advance

do:

return str(primarykeys)

Also there are Python modules for just this. Here is the very first link 
from Google:

http://pypi.python.org/pypi/python-json

I have used this one personally and have been very satisfied with it. 
There is another one (CJSON?) which is similar, but written in C, for 
when performance may be an issue.

Hope this helps.

Adonis



More information about the Python-list mailing list