JSON2TSV(1)

πŸ“¦ version 1.2, in nixos 26.05, nixpkgs unstable

🌸 NAME 🌸

json2tsv β€” convert JSON to TSV or separated output πŸ’–

okay bestie, this is the json2tsv(1) page from the General Commands Manual, and it is giving tiny-but-powerful JSON-to-TSV energy. No cap.

✨ SYNOPSIS ✨

json2tsv [-n] [-r] [-u] [-F fs] [-R rs]

πŸ’– DESCRIPTION πŸ’–

json2tsv reads JSON data from stdin. It outputs each JSON type to a TAB-Separated Value format per line. Simple, iconic, done. πŸ’…

The options are as follows:

  • -n β€” Show the indices for array types (by default off).
  • -r β€” Show all control-characters (by default off).
  • -u β€” Unbuffered: flush output after printing each value (by default off).
  • -F fs β€” Use fs as the field separator. The default is a TAB character.
  • -R rs β€” Use rs as the record separator. The default is a newline character.

πŸŽ€ SEPARATOR CHARACTERS πŸŽ€

The fs or rs separators can be specified in the following formats:

  • \\ β€” for a backslash character.
  • \n β€” for a newline character.
  • \r β€” for a carriage return character.
  • \t β€” for a TAB character.
  • \xXX β€” for a character specified in the hexadecimal format as XX.
  • \NNN β€” for a character specified in the octal format as NNN.

Otherwise: if a single character is specified this character will be used. If more than one character is specified it will be parsed as a number using the format supported by strtol(3) with base set to 0 and this character is the index in the ASCII table.

🌈 OUTPUT FORMAT 🌈

The output format per node is:

nodename<FIELD SEPARATOR>type<FIELD SEPARATOR>value<RECORD SEPARATOR>

Control-characters such as a newline, TAB and backslash (\n, \t and \\) are escaped in the nodename and value fields unless a -F or -R option is specified.

When the -F or -R option is specified then the separator characters are removed from the output. TABs or newlines are printed unless they are set as a separator. Other control-characters are removed, unless the option -r is set.

The type field is a single byte and can be:

  • a for array
  • b for bool
  • n for number
  • o for object
  • s for string
  • ? for null

⭐ EXIT STATUS ⭐

json2tsv exits with the exit status 0 on success, 1 on a parse error, 2 when out of memory or a read/write error or 3 with an usage error.

πŸ”₯ EXAMPLES πŸ”₯

First example, filter like a boss:

json2tsv < input.json | awk -F '\t' '$1 == ".url" { print $3 }'

To filter without having to unescape characters the -F and -R options can be used. In the example below it uses the ASCII character 0x1f (Unit Separator) as the field separator and the ASCII character 0x1e (Record Separator) as the record separator. Additionally the -r option is used so control-characters are printed.

json2tsv -r -F '\x1f' -R '\x1e' < input.json | \
        awk '
        BEGIN {
                FS = "\x1f"; RS = "\x1e";
        }
        $1 == ".url" {
                print $3;
        }'

The example can be simplified using the convenience wrapper shellscript jaq(1):

jaq '$1 == ".url" { print $3 }' < input.json

🌺 SEE ALSO 🌺

awk(1), jaq(1)

πŸ‘‘ AUTHORS πŸ‘‘

Hiltjo Posthuma <hiltjo@codemadness.org>

πŸ’€ CAVEATS πŸ’€

Characters in object keys such as a dot or brackets are not escaped in the TSV output, this can change the meaning of the nodename field. That is a real one, bestie β€” keep an eye on your keys.

The JSON parser handles all valid JSON. It also allows some invalid JSON extensions: it does not do a complete validation on numbers and is not strict with handling unicode input. See also RFC 8259 section 9. Parsers.

The maximum depth of objects or arrays is hard-coded to 64 levels deep.


This page’s source/version is Debian and the date is April 17, 2023 β€” mwah, bestie πŸ’‹βœ¨

πŸ“¦ where this page comes from