This Python script converts a folder of .json
files into Markdown .md
files using a specified template.
I built it to convert Reddit post data into Obsidian-friendly Markdown files, but it's easily adaptable for any JSON source data.
- Extracts Reddit post data from JSON and formats it into Markdown.
- Uses a template where
{{token}}
placeholders are replaced with actual values. - Skips empty or removed posts.
- Avoids overwriting existing Markdown files.
- Cleans up unnecessary characters in post content.
- Truncates filenames to 50 characters while keeping words intact.
- Outputs a summary of processed, skipped, and converted files.
- Can include the original post and a saved or upvoted reply in the output.
Ensure Python 3 is installed on your system.
- Place the
.json
files in a folder. - Create a Markdown template for each file (
template.md
). - Create a Markdown template for a post inside the file. (
template.post.md
). - Prepare an output folder for the Markdown files.
Use the following command:
python json2md.py <json_folder> <md_folder> <file_template> <post_template>
python json2md.py ./json_files ./markdown_output ./template.md ./template.post.md
Parameter | Description |
---|---|
<json_folder> |
Path to the folder containing JSON files. |
<md_folder> |
Path to the folder where Markdown files go. |
<file_template> |
Path to the Markdown file template file. |
<post_template> |
Path to the Markdown post template file. |
Your template file (template.md
) should include placeholders like:
---
original post: {{op_permalink}}
saved post: {{saved_permalink}}
subreddit: {{subreddit}}
created: {{op_created}}
---
# {{title}}
{{op_body}}
{{saved_post}}
Your post template (template.post.md
) should include placeholders like:
## Saved Comment
{{saved_body}}
[By {{saved_author}} on {{saved_created_time}}]({{saved_permalink}})
After running, the script prints a summary:
📂 Total JSON files processed: 100
✅ Successfully converted: 85
⚠️ Skipped (existing files): 10
🚫 Skipped (empty or removed content): 5
- If the script encounters unexpected JSON structures, it skips them.
- If
<json_folder>
contains non-JSON files, they are ignored. - If
<md_folder>
doesn't exist, it will be created.