Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > converting JSON to string

Reply
Thread Tools

converting JSON to string

 
 
Gowri
Guest
Posts: n/a
 
      01-12-2008
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
 
Reply With Quote
 
 
 
 
Adonis Vargas
Guest
Posts: n/a
 
      01-12-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
 
Reply With Quote
 
 
 
 
Gowri
Guest
Posts: n/a
 
      01-12-2008
Hi Adonis,

Thanks so much. Appreciate it
 
Reply With Quote
 
Gowri
Guest
Posts: n/a
 
      01-12-2008
On Jan 11, 7:21 pm, Adonis Vargas <adon...@REMOVETHISearthlink.net>
wrote:
> 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


Actually, I have one other problem after all this. I see that if I try
to construct JSON output as above, it is of the form
[{'isbn': u'1-56592-724-9', 'title': u'The Cathedral & the Bazaar'},
{'isbn': u'1-56592-051-1', 'title': u'Making TeX Work'}]
The extra 'u' seems to causing syntax error in JavaScript which is not
able to parse this response string. Any idea how I can fix this?
 
Reply With Quote
 
Jeroen Ruigrok van der Werven
Guest
Posts: n/a
 
      01-12-2008
-On [20080112 08:38], Gowri () wrote:
>Actually, I have one other problem after all this. I see that if I try
>to construct JSON output as above, it is of the form
>[{'isbn': u'1-56592-724-9', 'title': u'The Cathedral & the Bazaar'},
>{'isbn': u'1-56592-051-1', 'title': u'Making TeX Work'}]
>The extra 'u' seems to causing syntax error in JavaScript which is not
>able to parse this response string. Any idea how I can fix this?


JSON does not support Unicode in the sense of allowing raw Unicode codepoints.
Instead JSON uses a \uNNNN scheme to encode Unicode characters (a bit flawed
to limit it to four hexadecimal digits though, it leaves the whole CJK Unified
Ideographs Extension B out of scope.).

I use simplejson along with lxml/ElementTree for my JSON<>XML needs.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフãƒ*ック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
Any road leads to the end of the world...
 
Reply With Quote
 
Gowri
Guest
Posts: n/a
 
      01-13-2008
On Jan 12, 2:58 am, Jeroen Ruigrok van der Werven <asmo...@in-
nomine.org> wrote:
> -On [20080112 08:38], Gowri (gowr...@gmail.com) wrote:
>
> >Actually, I have one other problem after all this. I see that if I try
> >to construct JSON output as above, it is of the form
> >[{'isbn': u'1-56592-724-9', 'title': u'The Cathedral & the Bazaar'},
> >{'isbn': u'1-56592-051-1', 'title': u'Making TeX Work'}]
> >The extra 'u' seems to causing syntax error in JavaScript which is not
> >able to parse this response string. Any idea how I can fix this?

>
> JSON does not support Unicode in the sense of allowing raw Unicode codepoints.
> Instead JSON uses a \uNNNN scheme to encode Unicode characters (a bit flawed
> to limit it to four hexadecimal digits though, it leaves the whole CJK Unified
> Ideographs Extension B out of scope.).
>
> I use simplejson along with lxml/ElementTree for my JSON<>XML needs.
>
> --
> Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
> $B%$%'%k!<%s(B $B%i%&%U%m%C%/(B $B%t%!%s(B $B%G%k(B $B%&%'%k%t%'%s(Bhttp://www.in-nomine.org/|http://www.rangaku.org/
> Any road leads to the end of the world...


Thanks Jeroen. That helped a lot
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
suppressing bad characters in output PCDATA (converting JSON to XML) Adam Funk Python 6 12-02-2011 10:30 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuðrul Topuz ASP General 1 06-27-2008 11:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57