0

I need to convert XML to json without root in python. Here is an example of XML

  <?xml version="1.0" encoding="UTF-8"?>
<root>
  <row>
    <Member_ID>926494</Member_ID>
    <First_Name>Corissa</First_Name>
    <Last_Name>Aguiler</Last_Name>
    <Gender>F</Gender>
    <Age>39</Age>
    <Height>5,3</Height>
    <Weight>130</Weight>
    <Hours_Sleep>8</Hours_Sleep>
    <Calories_Consumed>2501</Calories_Consumed>
    <Exercise_Calories_Burned>990</Exercise_Calories_Burned>
    <Date>9/11/2017</Date>
  </row>
</root>

I need to convert into JSON in the following format

   {
    "Member_ID": 926494,
    "First_Name": "Corissa",
    "Last_Name": "Aguiler",
    "Gender": "F",
    "Age": 39,
    "Height": "5,3",
    "Weight": 130,
    "Hours_Sleep": 8,
    "Calories_Consumed": 2501,
    "Exercise_Calories_Burned": 990,
    "Date": "9/11/2017"
  },

I am trying to use the parker convention from xmljson library but all the examples I'm finding are using string as input. I can't seem to figure out how to pass the actual .xml file instead of a string

For example:

from xmljson import parker, Parker
from xml.etree.ElementTree import fromstring
from json import dumps
dumps(parker.data(fromstring('<x><a>1</a><b>2</b></x>')))
'{"a": 1, "b": 2}'

1 Answer 1

1

You can use standart xml library to parse it to dict and then dump dict to json if it needed:

xml_raw = """<?xml version="1.0" encoding="UTF-8"?>
<root>
  <row>
    <Member_ID>926494</Member_ID>
    <First_Name>Corissa</First_Name>
    <Last_Name>Aguiler</Last_Name>
    <Gender>F</Gender>
    <Age>39</Age>
    <Height>5,3</Height>
    <Weight>130</Weight>
    <Hours_Sleep>8</Hours_Sleep>
    <Calories_Consumed>2501</Calories_Consumed>
    <Exercise_Calories_Burned>990</Exercise_Calories_Burned>
    <Date>9/11/2017</Date>
  </row>
  <row>
    <Member_ID>926494</Member_ID>
    <First_Name>Corissa</First_Name>
    <Last_Name>Aguiler</Last_Name>
    <Gender>F</Gender>
    <Age>39</Age>
    <Height>5,3</Height>
    <Weight>130</Weight>
    <Hours_Sleep>8</Hours_Sleep>
    <Calories_Consumed>2501</Calories_Consumed>
    <Exercise_Calories_Burned>990</Exercise_Calories_Burned>
    <Date>9/11/2017</Date>
  </row>
</root>"""

import xml.etree.ElementTree as ET

root = ET.fromstring(xml_raw)

xml_dict_list = list()
for row in root.findall('.//row'):
    xml_dict = dict()
    for item in row.findall('./*'):
        xml_dict[item.tag] = item.text
    xml_dict_list.append(xml_dict)

print('dict ->', xml_dict_list)
import json

json_str = json.dumps(xml_dict_list)
print('str ->', json_str)

OUTPUT:

dict -> [{'Member_ID': '926494', 'First_Name': 'Corissa', 'Last_Name': 'Aguiler', 'Gender': 'F', 'Age': '39', 'Height': '5,3', 'Weight': '130', 'Hours_Sleep': '8', 'Calories_Consumed': '2501', 'Exercise_Calories_Burned': '990', 'Date': '9/11/2017'}, {'Member_ID': '926494', 'First_Name': 'Corissa', 'Last_Name': 'Aguiler', 'Gender': 'F', 'Age': '39', 'Height': '5,3', 'Weight': '130', 'Hours_Sleep': '8', 'Calories_Consumed': '2501', 'Exercise_Calories_Burned': '990', 'Date': '9/11/2017'}]
str -> [{"Member_ID": "926494", "First_Name": "Corissa", "Last_Name": "Aguiler", "Gender": "F", "Age": "39", "Height": "5,3", "Weight": "130", "Hours_Sleep": "8", "Calories_Consumed": "2501", "Exercise_Calories_Burned": "990", "Date": "9/11/2017"}, {"Member_ID": "926494", "First_Name": "Corissa", "Last_Name": "Aguiler", "Gender": "F", "Age": "39", "Height": "5,3", "Weight": "130", "Hours_Sleep": "8", "Calories_Consumed": "2501", "Exercise_Calories_Burned": "990", "Date": "9/11/2017"}]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works. Just one more question, how can I iterate over more than one row. When I run this code, it only provides the value of the first row of the xml.
@SamirKhan for row in root.findall('.//row'): ... for item in row.findall('./*'): ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.