Turn JSON into a Markdown table
Goal: Read a JSON file, keep only the records you care about, and render them as a Markdown table.
Prerequisites: The built-in json module. JSON has no built-in input format, so read the file with -I raw to get its text as a single string.
Query
$ mq -I raw 'import "json" | json::json_parse | get("users") | filter(fn(u): u["active"];) | json::json_to_markdown_table' users.json
Input (users.json)
{
"users": [
{ "name": "Alice", "role": "admin", "active": true },
{ "name": "Bob", "role": "dev", "active": false },
{ "name": "Carol", "role": "dev", "active": true }
]
}
Output
| name | role | active |
| --- | --- | --- |
| Alice | admin | true |
| Carol | dev | true |
Notes
json_parsereturns dicts and arrays, so the usual functions apply:get("users")reads a key,filterandmapwork on the array.- Drop the
filterto render every record.json_to_markdown_tabletakes the column names from the first record’s keys. - To get JSON back instead of a table, use
-F json. This lists the names:mq -I raw -F json 'import "json" | json::json_parse | get("users") | map(fn(u): u["name"];)' users.jsonreturns["Alice", "Bob", "Carol"]. json::json_stringify(data)turns a value back into a JSON string, for example to embed it in a larger string.