4

I want to fetch JSON data from a given url

http://www.deanclatworthy.com/imdb/?=The+Green+Mile

and convert the JSON data into XML. I have used urllib and json to convert JSON objects into python dictionary.

Here is my code:

import json

json_string = '{"imdbid":"tt0120689","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","genres":"Crime,Drama,Fantasy,Mystery","languages":"English ,French","country":"USA","votes":"281023","stv":0,"series":0,"rating":"8.4","title":"The Green Mile","year":"1999","usascreens":2875,"ukscreens":340}'

new_python_object = json.loads(json_string)
print(json_string)
print()
print (new_python_object)

And the result:

{"imdbid":"tt0120689","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","genres":"Crime,Drama,Fantasy,Mystery","languages":"English ,French","country":"USA","votes":"281023","stv":0,"series":0,"rating":"8.4","title":"The Green Mile","year":"1999","usascreens":2875,"ukscreens":340}

{'ukscreens': 340, 'rating': '8.4', 'genres': 'Crime,Drama,Fantasy,Mystery', 'title': 'The Green Mile', 'series': 0, 'imdbid': 'tt0120689', 'year': '1999', 'votes': '281023', 'languages': 'English ,French', 'stv': 0, 'country': 'USA', 'usascreens': 2875, 'imdburl': 'http://www.imdb.com/title/tt0120689/'}
Jay
  • 16,941
  • 11
  • 49
  • 71
  • http://stackoverflow.com/questions/1019895/serialize-python-dictionary-to-xml – dm03514 Sep 27 '12 at 18:20
  • Reading in JSON and converting it to objects in your language of choice is generally pretty easy. But now you have to sit down and define what the translation is between your JSON keywords and XML keywords (for the specific case of this data source), and that will be some hard work, even before you write the code to spit out the XML. – Hot Licks Sep 27 '12 at 18:29

3 Answers3

1

Using the requests and dict2xml libraries:

>>> import requests
>>> r = requests.get("http://www.deanclatworthy.com/imdb/?q=The+Green+Mile")
>>> import dict2xml
>>> xml = dict2xml.dict2xml(r.json)
>>> print xml
<country>USA</country>
<genres>Crime,Drama,Fantasy,Mystery</genres>
<imdbid>tt0120689</imdbid>
<imdburl>http://www.imdb.com/title/tt0120689/</imdburl>
<languages>English,French</languages>
<rating>8.5</rating>
<runtime>189min</runtime>
<series>0</series>
<stv>0</stv>
<title>The Green Mile</title>
<ukscreens>340</ukscreens>
<usascreens>2875</usascreens>
<votes>344054</votes>
<year>1999</year>
jterrace
  • 57,555
  • 21
  • 140
  • 184
0

Now that you have a usable python dictionary you have to write it to xml.

What does your xml format look like? Python provides some tools in the standard library for writing/working with xml. http://docs.python.org/library/xml.dom.minidom.html

Writing will require some effort because python doesn't know the structure of your xml document. A little google research can go a long way.

dm03514
  • 50,477
  • 16
  • 96
  • 131
0

There is a very entry on ActiveState's recipes page (by Cory Fabre) for converting a python dict to and from XML. I've been using it a bit lately and it seems to work quite nicely. Here is the link:

http://code.activestate.com/recipes/573463-converting-xml-to-dictionary-and-back/

This could be useful if you are lucky enough that your JSON structure maps pretty well to an XML structure.

Brad Campbell
  • 2,525
  • 2
  • 19
  • 18