Work with JSON and XML in Python
JSON and XML are the most widely used data interchange formats for APIs, configuration files, and data storage. Python includes built-in support for both.
JSON with the json Module
Read and Write JSON
import json
# Read JSON from file
with open("data.json", encoding='utf-8') as f:
data = json.load(f)
print(data)
print(type(data)) # dict or list
# Write JSON to file
data["updated"] = "2024-06-15"
with open("data_new.json", "w", encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# To/from string
json_str = json.dumps({"name": "Alice", "age": 30}, indent=2)
obj = json.loads(json_str)
print(obj["name"])
Filter and Transform JSON
import json
with open("users.json", encoding='utf-8') as f:
users = json.load(f)
# Filter active users over 18
active = [u for u in users if u.get("active") and u.get("age", 0) >= 18]
# Transform structure
summary = [{"id": u["id"], "name": u["name"]} for u in active]
with open("summary.json", "w", encoding='utf-8') as f:
json.dump(summary, f, indent=2, ensure_ascii=False)
print(f"Filtered: {len(summary)} of {len(users)}")
JSON Lines (JSONL) — One Object per Line
import json
records = [
{"id": 1, "name": "Alice", "city": "New York"},
{"id": 2, "name": "Bob", "city": "London"},
{"id": 3, "name": "Charlie", "city": "Sydney"},
]
with open("data.jsonl", "w", encoding='utf-8') as f:
for r in records:
f.write(json.dumps(r) + "\n")
with open("data.jsonl", encoding='utf-8') as f:
loaded = [json.loads(line) for line in f if line.strip()]
print(f"Records: {len(loaded)}")
XML with xml.etree.ElementTree
Read XML
import xml.etree.ElementTree as ET
tree = ET.parse("catalog.xml")
root = tree.getroot()
print(f"Root: {root.tag}")
for child in root:
print(f" {child.tag}: {child.text} attrs={child.attrib}")
# XPath-style search
for product in root.findall(".//product"):
name = product.find("name").text
price = product.find("price").text
print(f" {name}: {price}")
Create and Write XML
import xml.etree.ElementTree as ET
from xml.dom import minidom
def create_xml_catalog(products, output):
root = ET.Element("catalog")
root.set("version", "1.0")
for p in products:
elem = ET.SubElement(root, "product")
elem.set("id", str(p["id"]))
ET.SubElement(elem, "name").text = p["name"]
ET.SubElement(elem, "price").text = str(p["price"])
ET.SubElement(elem, "stock").text = str(p["stock"])
xml_str = minidom.parseString(ET.tostring(root, encoding='unicode')).toprettyxml(indent=" ")
with open(output, "w", encoding="utf-8") as f:
f.write(xml_str)
print(f"XML created: {output}")
create_xml_catalog([
{"id": 1, "name": "Widget A", "price": 9.99, "stock": 150},
{"id": 2, "name": "Widget B", "price": 24.99, "stock": 80},
], "catalog.xml")
xmltodict — XML as Python Dictionary
pip install xmltodict
import xmltodict, json
with open("catalog.xml", encoding='utf-8') as f:
data = xmltodict.parse(f.read())
# It's now a regular Python dictionary
print(data["catalog"]["product"])
# XML to JSON
with open("catalog.json", "w", encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# dict back to XML
xml_str = xmltodict.unparse(data, pretty=True)
with open("new.xml", "w", encoding='utf-8') as f:
f.write(xml_str)
Convert JSON to XML
import json, xmltodict
def json_to_xml(json_path, xml_path, root_key="data"):
with open(json_path, encoding='utf-8') as f:
data = json.load(f)
wrapped = {root_key: data}
xml_str = xmltodict.unparse(wrapped, pretty=True)
with open(xml_path, "w", encoding='utf-8') as f:
f.write(xml_str)
print(f"Converted: {json_path} -> {xml_path}")
json_to_xml("data.json", "data.xml")
Additional Resource
For converting JSON, XML and CSV files between formats without any coding, use KaijuConverter — free and no registration required.
Related conversions
Document conversions that follow this topic naturally: