Skip to main content

Markdown to dict and json deserializer

Project description

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.

Why would you want to do this?

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 datastructure to extract data-like structures from a Markdown document.

This tool was built to allow easy 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 njvack@freshforever.net 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_to_json-2.1.0.tar.gz (50.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_to_json-2.1.0-py3-none-any.whl (52.3 kB view details)

Uploaded Python 3

File details

Details for the file markdown_to_json-2.1.0.tar.gz.

File metadata

  • Download URL: markdown_to_json-2.1.0.tar.gz
  • Upload date:
  • Size: 50.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for markdown_to_json-2.1.0.tar.gz
Algorithm Hash digest
SHA256 ea02313f7c5e8d05033d7a2b4e7c891246bc8f6391e1681760579687e6b0ba68
MD5 b234e1c59a091bf668f41b3b5faa5877
BLAKE2b-256 255061b5710d3201ee9e14f599f06d886dba3958423b99428179d3f6f7206a63

See more details on using hashes here.

File details

Details for the file markdown_to_json-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_to_json-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44a17e3ff42af4f049fa2a6a86efbe30e27dcf8401c7ad1772b97b2d396d88f8
MD5 013e1c9118b543b53c4b25c57d039ece
BLAKE2b-256 cebf4ac898c83ec46fa9376a8bce125057e9b991760efbe7631813631ed3aade

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page