fmd2json

fmd2json short description
Synopsis
Convert Markdown files with YAML frontmatter to JSON.
% cat article.md
---
title: Hello
tags:
- go
- cli
---
This is the body.
% fmd2json article.md
{"title":"Hello","tags":["go","cli"],"filename":"article","body":"This is the body.\n","mtime":"2025-01-01T00:00:00+09:00"}
Description
fmd2json reads Markdown files with YAML frontmatter and outputs JSON. Frontmatter properties are included as-is, along with default properties:
dir — directory path of the input file (omitted when empty or current directory, always uses / as separator)
filename — file name without .md extension
body — content after frontmatter (or entire content if no frontmatter)
mtime — file modification time in RFC 3339 format
Multiple files produce newline-delimited JSON (ndjson):
% fmd2json docs/a.md docs/b.md
{"dir":"docs","filename":"a",...}
{"dir":"docs","filename":"b",...}
Read from stdin with -:
% cat article.md | fmd2json -
{"filename":"","body":"...","..."}
Use -filename to specify the filename when reading from stdin:
% cat article.md | fmd2json -filename article.md -
{"filename":"article","body":"...","..."}
Read a file list from stdin (when no arguments given):
% find . -name '*.md' | fmd2json
{"dir":"./docs","filename":"article1",...}
{"dir":"./docs","filename":"article2",...}
--jq option
Filter and transform JSON output using jq expressions (powered by gojq):
% fmd2json --jq '.filename' a.md b.md
"a"
"b"
% fmd2json --jq '{title: .filename, size: (.body | length)}' article.md
{"title":"article","size":42}
By default, string values are output as JSON-encoded strings (with quotes and escapes), which keeps each result on a single line even if the value contains newlines. This is safe for multi-file processing:
% fmd2json --jq '.body' a.md b.md
"first line\nsecond line\n"
"another body\n"
Use --raw-output (or -r) to output raw strings without JSON encoding, similar to jq -r:
% fmd2json --jq '.filename' -r a.md b.md
a
b
Installation
# Install the latest version. (Install it into ./bin/ by default).
% curl -sfL https://raw.githubusercontent.com/Songmu/fmd2json/main/install.sh | sh -s
# Specify installation directory ($(go env GOPATH)/bin/) and version.
% curl -sfL https://raw.githubusercontent.com/Songmu/fmd2json/main/install.sh | sh -s -- -b $(go env GOPATH)/bin [vX.Y.Z]
# In alpine linux (as it does not come with curl by default)
% wget -O - -q https://raw.githubusercontent.com/Songmu/fmd2json/main/install.sh | sh -s [vX.Y.Z]
# go install
% go install github.com/Songmu/fmd2json/cmd/fmd2json@latest
Author
Songmu