Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Working with JSON and XML

Learning Goals

By the end of this section you will:

  • understand how to convert JSON strings and files into Python dictionaries

  • understand how to convert Python dictionaries into JSON strings and files

  • understand how to extract data from an XML string and file using Python

  • understand how to save Python data to an XML file

JSON

JSON (JavaScript Object Notation) is a lightweight data format used to store and exchange data. It’s easy for humans to read and write, and easy for machines to parse and generate.

A JSON object is similar to a Python dictionary.

{
  "name": "Alice",
  "age": 25,
  "is_student": true,
  "courses": ["Math", "Science"]
}

Python and JSON

Python provides a built-in module called json to work with JSON data.

JSON to Python

Convert a JSON-formatted string into a Python dictionary.

import json

json_string = '{"name": "Alice", "age": 25}'
python_dict = json.loads(json_string)
print(python_dict)
print(python_dict['name'])

Python to JSON

Convert a Python dictionary into a JSON-formatted string.

import json

data = {'name': 'Bob', 'age': 30, 'is_student': False}
json_string = json.dumps(data)
print(json_string)

Working with JSON Files

Reading from a JSON file

Open a JSON file, reads its JSON content, then convert it into a Python object, depending on the JSON file format:

import json

with open('data.json', 'r') as file:
    data = json.load(file)
    print(data)

Writing to a JSON file

Convert a Python dictionary into a JSON-formatted string and write it to a JSON file.

import json

data = {'name': 'Carol', 'age': 28}
with open('output.json', 'w') as file:
    json.dump(data, file, indent=4)

Summary

TaskMethod
JSON String  →  Python Objectjson.loads()
Python Object  →  JSON Stringjson.dumps()
JSON File  →  Python Objectjson.load()
Python Object  →  JSON Filejson.dump()

XML

XML (eXtensible Markup Language) is a markup language used to store and transport data. It’s similar to HTML but focuses on describing data rather than displaying it.

Example XML:

<student>
    <name>Alice</name>
    <age>25</age>
    <courses>
        <course>Math</course>
        <course>Science</course>
    </courses>
</student>

Python and XML

Python includes a built-in module called xml.etree.ElementTree (often shortened to ET) for parsing and creating XML data.

Parsing XML from a String

This code parses an XML string into a Python XML element.

import xml.etree.ElementTree as ET

xml_data = """
<student>
    <name>Alice</name>
    <age>25</age>
</student>
"""

root = ET.fromstring(xml_data)
print(root.tag)  # Output: student
print(root.find('name').text)  # Output: Alice
print(root.find('age').text)   # Output: 25

Parsing XML from a File

Create an XML element from a string and access its child elements.

student.xml:

<student>
    <name>Bob</name>
    <age>22</age>
</student>

Python code:

tree = ET.parse('student.xml')
root = tree.getroot()

print(root.find('name').text)  # Output: Bob
print(root.find('age').text)   # Output: 22

Looping Through Elements

Parse an XML string containing multiple students, then loops through each <student> element to print their name and age in a formatted sentence.

xml_data = """
<students>
    <student>
        <name>Alice</name>
        <age>25</age>
    </student>
    <student>
        <name>Bob</name>
        <age>22</age>
    </student>
</students>
"""

root = ET.fromstring(xml_data)

for student in root.findall('student'):
    name = student.find('name').text
    age = student.find('age').text
    print(f"{name} is {age} years old")

Creating an XML Document

import xml.etree.ElementTree as ET

root = ET.Element("student")
name = ET.SubElement(root, "name")
name.text = "Carol"
age = ET.SubElement(root, "age")
age.text = "23"

tree = ET.ElementTree(root)
tree.write("new_student.xml", encoding="utf-8", xml_declaration=True)

Summary

TaskMethod
Parse from stringET.fromstring(xml_string)
Parse from fileET.parse(file)
Find elementelement.find('tag')
Find all elementselement.findall('tag')
Get element textelement.text
Write to XML filetree.write('file.xml')