json – Python – xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
I am trying to convert a xml file to a json file. I am doing this by using ElementTree and parsing through it. I am stuck on this error that I am getting though and I am not sure how to fix it.
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
import xml.etree.ElementTree as ET
import json
def xml_to_json(xml_file, json_file):
tree = ET.parse(xml_file)
root = tree.getroot()
data = {}
for elem in root:
data[elem.tag] = elem.text
with open(json_file, 'w') as f:
json.dump(data, f)
xml_to_json('input.xml', 'output9999.json')
I tried making a UTF addition to my tree parse but I continued to get the same error. I tried using different inputs, same errors as well.
Here is the contents of my input, input.xml.
{“timestamp”:”2023-03-06T00:14:52.2485541-05:00″,”hash”:{“body_md5″:”276bbb20c29087e88db63899fd8f9129″,”body_mmh3″:”1084612514″,”body_sha256″:”5b61b0c2032b4aa9519d65cc98c6416c12415e02c7fbbaa1be5121dc75162edb”,”body_simhash”:”17956186510721919950″,”header_md5″:”61236511ca06631d69ac2aa03e2b2ed6″,”header_mmh3″:”1266527742″,”header_sha256″:”64a1a17658580224e1a593aa079f934c0078f83da2331d3cbcb924f4930e2786″,”header_simhash”:”9814106780755273709″},”port”:”443″,”url”:”https://google.com:443″,”input”:”google.com“,”location”:”https://www.google.com/”,”title”:”301 Moved”,”scheme”:”https”,”webserver”:”gws”,”content_type”:”text/html”,”method”:”GET”,”host”:”142.250.190.142″,”path”:”/”,”time”:”212.2639ms”,”a”:[“142.250.190.142″,”2607:f8b0:4009:802::200e”],”tech”:[“Google Web Server”,”HTTP/3″],”words”:9,”lines”:7,”status_code”:301,”content_length”:220,”failed”:false}
Read more here: Source link
