JSON2TSV(1)
πΈ 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β Usefsas the field separator. The default is a TAB character.-R rsβ Usersas 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 asXX.\NNNβ for a character specified in the octal format asNNN.
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:
afor arraybfor boolnfor numberofor objectsfor 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 πΊ
π 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
- nixos 26.05: json2tsv 1.2
- nixpkgs unstable: json2tsv 1.2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms