How to convert XML to JSON in Python [duplicate]

Tags: python xml json
By : Geuis
Source: Stackoverflow.com
Question!

Possible Duplicate:
Converting XML to JSON using Python?

I am importing an XML feed and trying to convert it to JSON for output. I'm getting this error:

TypeError: <xml.dom.minidom.Document instance at 0x72787d8> is not JSON serializable

Unfortunately I know next to nothing about Python. I'm developing this on the Google App Engine. I could use some help, because my little 2 hour hack that was going so well is now on its 3rd day.

XML data:

<?xml version="1.0" ?><eveapi version="2">
  <currentTime>2009-01-25 15:03:27</currentTime>
  <result>
    <rowset columns="name,characterID,corporationName,corporationID" key="characterID" name="characters">
      <row characterID="999999" corporationID="999999" corporationName="filler data" name="someName"/>
    </rowset>
  </result>
  <cachedUntil>2009-01-25 15:04:55</cachedUntil>

</eveapi>

My code:

class doproxy(webapp.RequestHandler):
def get(self):
    apiurl = 'http://api.eve-online.com'

    path = self.request.get('path');
    type = self.request.get('type');
    args = '&'+self.request.get('args');

    #assemble api url
    url = apiurl+path

    #do GET request     
    if type == 'get':
        result = urlfetch.fetch(url,'','get');

    #do POST request
    if type == 'post':
        result = urlfetch.fetch(url,args,'post');   

    if result.status_code == 200:               
        dom = minidom.parseString( result.content ) #.encode( "utf-8" ) )
        dom2json = simplejson.dump(dom,"utf-8")
By : Geuis


Answers

I'm quickly coming to the opinion that Python is potentially a great language, but that none of its users know how to actually document anything in a clear and concise way.

The attitude of the question isn't going to help with getting answers from these same Python users.

As is mentioned in the answers to this related question, there is no 1-to-1 correspondence between XML and JSON so the conversion can't be done automatically.

In the documentation for simplejson you can find the list of types that it's able to serialize, which are basically the native Python types (dict, list, unicode, int, float, True/False, None).

So, you have to create a Python data structure containing only these types, which you will then give to simplejson.dump().

By : dF.


I just posted in a similar question my own approach to transform MySQL hierarchical data (adjacency list) into a menu (HTML)

It does not use recursion. And it requires a single query to the database.

Read more at

http://stackoverflow.com/questions/2871861#3368622

Thanks.

By : J. Bruni


Another simple way you can generate hierarchy if you don't want to use nested sets is to use a simple text string in front.

Item B
    Item B-1
        Item B-1-2
        Item B-1-1
Item A
    SubItem A-1
    SubItem A-2
Item C

Would become

1 Item B
  1.1 Item B1
    1.1.1 Item B11
    1.1.2 Item B12
2 Item A
  2.1 Item A1
  2.2 Item B2
3 Item C

The digit in front of each item could be stored in a field and parsed based on length (representing the depth of where it is) to tell you everything you need to know about where it goes.

I use nested set hierarchies for more complicated stuff that requires calculation,e tc, but I find this approach has served well



This video can help you solving your question :)
By: admin