Skip to main content

Markdown to JSON converter

Description

A simple tool to convert Markdown (CommonMark dialect) data into JSON. It uses headings as JSON keys, and the stuff following headings as values. Lists are turned into arrays. Higher heading values yield nested JSON keys.

Is this for me?

If you have to ask that question, it probably isn't. Here are some cases where I'd recommend something else:

I want to parse arbitrary Markdown

In this case, I'd recommend the excellent markdown-it-py

I want to hand-write nested structures but hate writing JSON by hand

In this case, TOML might be what you're looking for. Python support is built-in as of 3.11. If I had known of TOML, I would never have written this package.

Nope, those situations don't cover me; I really want to parse Markdown into data

If you don't mind the loss of fidelity to the exact Markdown Document Object Model (DOM), you can get a simple python or json data structure to extract data-like structures from a subset of Markdown documents.

This tool was built to allow easier creation of dataset descriptions for the Brain Imaging Data Structure data sharing specification.

Installation

Non isolated install from pypi

pip install markdown-to-json
md_to_json --help

Isolated install with pipx if you only want the CLI

pipx install markdown-to-json
md_to_json --help

Install bleeding edge from github

pip install git+https://github.com/njvack/markdown-to-json/
python -m markdown_to_json --help
git clone https://github.com/njvack/markdown-to-json.git
cd markdown_to_json
./setup.py install

The package has no external requirements and has been tested python 3.6+.

Please use version 1 or 1.1 for python 2.x.

CLI Usage, md_to_json

Translate Markdown into JSON.

Usage:
  md_to_json [options] <markdown_file>
  md_to_json -h | --help

Options:
  -h --help     Show this screen
  --version     Print version number
  -o <file>     Save output to a file instead of stdout
  -i <val>      Indent nested JSON by this amount. Use a negative number for
                most compact possible JSON. the [default: 2]

Programmatic usage

import markdown_to_json
value = """
# Nested List

* Item 1
    * Item 1.1
* Item 2
"""

# The simple way:
dictified = markdown_to_json.dictify(value)
assert dictified == {'Nested List': ['Item 1', ['Item 1.1'], 'Item 2']}

# Or, if you want a json string
jsonified = markdown_to_json.jsonify(value)
assert jsonified == """{"Nested List": ["Item 1", ["Item 1.1"], "Item 2"]}"""

This translates a Markdown document into JSON as described in the example below.

Example

The Markdown:

# Description

This is an example file

# Authors

* Nate Vack
* Vendor Packages
    * docopt
    * CommonMark-py

# Versions

## Version 1

Here's something about Version 1; I said "Hooray!"

## Version 2

Here's something about Version 2

will translate to the JSON:

{
  "Description": "This is an example file",
  "Authors": ["Nate Vack", "Vendor Packages", ["docopt", "CommonMark-py"]],
  "Versions": {
    "Version 1": "Here's something about Version 1; I said \"Hooray!\"",
    "Version 2": "Here's something about Version 2"
  }
}

Credits

markdown_to_json was written by Nate Vack at the Center for Healthy Minds at the University of Wisconsin–Madison.

Maintenance development by Matthew Martin

This tool ships a few really excellent tools in its vendor directory:

docopt is copyright (c) 2012 Vladimir Keleshev, vladimir@keleshev.com

Upgraded to docopt-ng.

CommonMark-py is copyright Copyright (c) 2014, Bibek Kafle and Roland Shoemaker.

Cannot upgrade to 0.6.0 because of breaking changes in AST.

Release files for markdown-to-json 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markdown-to-json 2.1.2
File Size Uploaded
markdown_to_json-2.1.2.tar.gz 51.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for markdown-to-json 2.1.2
File Interpreter ABI Platform
markdown_to_json-2.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 104.2 kB

Release files / markdown_to_json-2.1.2.tar.gz

Download URL markdown_to_json-2.1.2.tar.gz
Size 51.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8fa5b4191c74a20297fe2622ee6dd202f82448d025af9c837732d45a64551d01
BLAKE2b-256 checksum
How to use checksums
7db7e333b0c8d3447b0e55a32a57beb15d456e33ea605312bfebb6e3dba29205
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / markdown_to_json-2.1.2-py3-none-any.whl

Download URL markdown_to_json-2.1.2-py3-none-any.whl
Size 52.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bda2c90082d93a6da051dbeda2ab46d79da5ed0590f5a5abba6c58dbc751be1b
BLAKE2b-256 checksum
How to use checksums
0b0aaffeb612dcd97a6842e59b118b7facd6cfaf1dc7509ad7a4d986c974d2da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

2.1.2 This release

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page