ssh — guest: ~ connected
guest@ludovic.toinel.com:~$ man json

json — format, validate and query JSON

SYNOPSIS

json [-c] [-r] [-q <path>] <json text> json [-c] [-r] [-q <path>] <file> json [-c] [-r] [-q <path>] (read piped input)

DESCRIPTION

Reads a JSON document — typed as an argument, stored in a file, or piped in — checks that it is valid and prints it indented by two spaces, with syntax colours. Invalid JSON is reported with the line and the column of the first error, and the offending line is shown with a marker.

Quote the JSON so the shell keeps it in one piece: json '{"a": 1}'.

OPTIONS

-c compact output: one line, no spaces (minify) -q <path> print only the value at <path> (see QUERY PATHS) -r raw strings: print a string result without its quotes -- end of options

QUERY PATHS

A path walks down the document, starting with a dot:

.name the value of key "name" .a.b nested keys .items[0] an array element (negative numbers count from the end) .items[] every element of an array (or every value of an object) .["my key"] a key that is not a plain word

Steps chain freely: .items[].name prints the name of every item, one result per line. A key that does not exist gives null.

HOW IT WORKS

The text is parsed by your browser's own JSON engine, the strict parser defined by the JSON standard (RFC 8259): double-quoted keys and strings, no trailing commas, no comments. When the engine rejects the text, a small scanner walks it again to find the exact line and column of the first mistake, since browsers do not agree on how they report it.

Everything runs locally: the document never leaves this page, so it is safe to paste an API response or a configuration file that holds secrets.

USE CASES

  • make a minified API response readable, or minify a document with -c;
  • find the missing comma or quote that breaks a configuration file;
  • pull one field out of a document: json -q .version package.json;
  • feed a value to another command: json -r -q .url data.json | qr.

NOTES

Numbers go through JavaScript: integers beyond 2^53 lose precision. Duplicate keys keep their last value.

EXAMPLES

json '{"name":"LudOS","tags":["shell","astro"]}' json -c '{ "a": 1, "b": [1, 2, 3] }' json -q .tags[0] '{"name":"LudOS","tags":["shell","astro"]}' json -q .items[].name data.json cat data.json | json -r -q .title

SEE ALSO

jwt, base64, urlencode, grep, cat, regex