XhCode Online Converter Tools

OPML To JSON Converter

Enter opml here:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Results:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
OPML To JSON

OPML (Outline Processor Markup Language) is an XML-based format used for representing outlines, commonly used in RSS feed subscriptions, task management, or hierarchical data. Converting OPML to JSON involves transforming the hierarchical structure of an OPML document into a JSON format for easier integration with web applications and other tools.

Example OPML:
Here's an example of an OPML file with RSS feed subscriptions:

xml

<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>My RSS Feeds</title>
</head>
<body>
<outline text="Technology">
<outline type="rss" text="Tech News" xmlUrl="https://technews.com/rss" />
<outline type="rss" text="Gadget Reviews" xmlUrl="https://gadgets.com/rss" />
</outline>
<outline text="Science">
<outline type="rss" text="Science Daily" xmlUrl="https://sciencedaily.com/rss" />
</outline>
</body>
</opml>
Converted JSON Output:
json

{
"head": {
"title": "My RSS Feeds"
},
"body": [
{
"text": "Technology",
"feeds": [
{
"type": "rss",
"text": "Tech News",
"xmlUrl": "https://technews.com/rss"
},
{
"type": "rss",
"text": "Gadget Reviews",
"xmlUrl": "https://gadgets.com/rss"
}
]
},
{
"text": "Science",
"feeds": [
{
"type": "rss",
"text": "Science Daily",
"xmlUrl": "https://sciencedaily.com/rss"
}
]
}
]
}
How to Convert OPML to JSON
1. Using JavaScript (For Web Use)
If you want to convert OPML to JSON in a browser using JavaScript, you can use the DOMParser to parse the XML content and then convert it into a JSON object.

JavaScript Code:
html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OPML to JSON</title>
</head>
<body>

<h2>Convert OPML to JSON</h2>
<input type="file" id="fileInput" />
<pre id="jsonOutput"></pre>

<script>
function parseOPMLToJSON(opml) {
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(opml, "application/xml");
let json = { head: {}, body: [] };

// Extract title from <head>
json.head.title = xmlDoc.getElementsByTagName("title")[0]?.textContent || "";

// Extract outlines from <body>
let outlines = xmlDoc.getElementsByTagName("outline");
let currentCategory = null;

for (let i = 0; i < outlines.length; i++) {
let outline = outlines[i];
let categoryText = outline.getAttribute("text");

if (categoryText && outline.childElementCount > 0) {
currentCategory = {
text: categoryText,
feeds: []
};
json.body.push(currentCategory);
}

if (currentCategory && outline.hasAttribute("xmlUrl")) {
currentCategory.feeds.push({
type: outline.getAttribute("type"),
text: outline.getAttribute("text"),
xmlUrl: outline.getAttribute("xmlUrl")
});
}
}

return json;
}

document.getElementById("fileInput").addEventListener("change", function(event) {
let file = event.target.files[0];
let reader = new FileReader();

reader.onload = function(e) {
let opmlContent = e.target.result;
let jsonData = parseOPMLToJSON(opmlContent);
document.getElementById("jsonOutput").textContent = JSON.stringify(jsonData, null, 2);
};

reader.readAsText(file);
});
</script>

</body>
</html>
Explanation:
File Input: The user uploads an OPML file using the file input (<input type="file">).
parseOPMLToJSON() Function: This function:
Parses the OPML XML content using DOMParser.
Extracts data from the <head> and <body> elements.
Converts the XML data into a nested JSON structure.
The JSON output is displayed in the <pre> tag.
Steps:
Save this code as an HTML file.
Open the file in a web browser.
Upload an OPML file using the file input to see the JSON conversion result.
2. Using Python (For Server-Side or Automated Conversion)
Python provides an easy way to work with XML through libraries like xml.etree.ElementTree. Below is an example Python script that converts OPML to JSON.

Python Script:
python

import xml.etree.ElementTree as ET
import json

def opml_to_json(opml_file):
tree = ET.parse(opml_file)
root = tree.getroot()

result = {
'head': {},
'body': []
}

# Extract head data (e.g., title)
result['head']['title'] = root.find('.//head/title').text if root.find('.//head/title') is not None else ''

# Extract body data (outlines)
for outline in root.findall('.//body/outline'):
category = outline.get('text')
if category:
category_data = {
'text': category,
'feeds': []
}
for child in outline.findall('outline'):
feed = {
'type': child.get('type'),
'text': child.get('text'),
'xmlUrl': child.get('xmlUrl')
}
category_data['feeds'].append(feed)
result['body'].append(category_data)

return json.dumps(result, indent=2)

# Usage
opml_file = 'example.opml'
json_data = opml_to_json(opml_file)

# Save to a JSON file
with open('output.json', 'w') as json_file:
json_file.write(json_data)

print("OPML has been converted to JSON and saved as 'output.json'.")
Explanation:
xml.etree.ElementTree: Used to parse the OPML XML file.
Extracts <head> and <body> Data: It retrieves the title from the <head> and processes each <outline> in the <body>.
JSON Conversion: The script converts the parsed data into a structured JSON format.
Steps:
Save the script to a .py file.
Replace 'example.opml' with the path to your OPML file.
Run the script, and the result will be saved in output.json.
3. Using Online Tools
If you prefer not to write code, you can use online tools to convert OPML to JSON:

OPML to JSON Converter:

Paste your OPML XML content into the tool.
The tool will automatically convert it into a JSON format.
ConvertJSON:

Upload your OPML file.
The tool will generate the corresponding JSON.
4. Manually Converting OPML to JSON
For small OPML files, you can manually transform the structure into JSON. The key is to map each <outline> element (and its nested outlines) to an array of JSON objects.

OPML:

xml

<opml version="2.0">
<head>
<title>My RSS Feeds</title>
</head>
<body>
<outline text="Technology">
<outline type="rss" text="Tech News" xmlUrl="https://technews.com/rss" />
<outline type="rss" text="Gadget Reviews" xmlUrl="https://gadgets.com/rss" />
</outline>
</body>
</opml>
Manually Written JSON:

json

{
"head": {
"title": "My RSS Feeds"
},
"body": [
{
"text": "Technology",
"feeds": [
{
"type": "rss",
"text": "Tech News",
"xmlUrl": "https://technews.com/rss"
},
{
"type": "rss",
"text": "Gadget Reviews",
"xmlUrl": "https://gadgets.com/rss"
}
]
}
]
}
Summary of Methods:
JavaScript: Use JavaScript and DOM parsing in the browser to convert OPML to JSON.
Python: Use xml.etree.ElementTree and json in Python to automate OPML to JSON conversion.
Online Tools: Use OPML to JSON Converter or ConvertJSON for quick, no-code conversion.
Manual Conversion: For small OPML files, manually convert the XML structure into a JSON object.