Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to convert XML to JSON
#2
You probably use Python 3 as you should Wink
There also a problem,most also write the xml file before read it.
import urllib.request
import json
import xmltodict

xml = urllib.request.urlopen('https://www.openhub.net/projects.xml?api_key=xxxxxxxxxxxxxx')
with open("response.xml", 'wb') as f_out:
    f_out.write(xml.read())  

with open("response.xml", 'r') as f:
	xmlString = f.read()
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)
print(jsonString)
Ideally don't use urllib at all,but Requests.
import requests
import json
import xmltodict

xml = requests.get('https://www.openhub.net/projects.xml?api_key=xxxxxxxxxxxxxx')
with open("response.xml", 'wb') as f_out:
    f_out.write(xml.content)

with open("response.xml", 'r') as f:
	xmlString = f.read()
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)
print(jsonString)
Reply


Messages In This Thread
Unable to convert XML to JSON - by priyanka - Jun-29-2018, 07:44 AM
RE: Unable to convert XML to JSON - by snippsat - Jun-29-2018, 09:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to convert browser generated xml to parse in BeautifulSoup Nik1811 0 327 Mar-22-2024, 01:37 PM
Last Post: Nik1811
  convert html table to json bhojendra 5 16,078 Jul-30-2019, 07:53 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020