JSON2TSV(1)

NAME
json2tsv - convert JSON to TSV or separated output

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.

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

    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.

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.