json2tsv
--------

Convert JSON to TSV or separated output.

json2tsv reads JSON data from stdin.  It outputs each JSON type to a TAB-
Separated Value format per line by default.


Build and install
-----------------

$ make
# make install


Dependencies
------------

- C compiler (C99).
- libc


Optional dependencies
---------------------

- POSIX make(1) (for Makefile).
- mandoc for documentation: https://mdocml.bsd.lv/


TAB-Separated Value format
--------------------------

By default the output format per line is:

	nodename<TAB>type<TAB>value<LF>

The nodename and value are escaped (\n, \t and \\).  Control-characters are
removed.

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

Filtering on the first field "nodename" is easy using awk for example.

See the json2tsv(1) man page for the full documentation of the options.


Example
-------

An usage example to parse JSON posts of reddit.com and format them to a
plain-text list using awk:


#!/bin/sh
curl -s -H 'User-Agent:' 'https://old.reddit.com/.json?raw_json=1&limit=100' | \
json2tsv | \
awk -F '\t' '
function show() {
	if (length(o["title"]) == 0)
		return;
	print n ". " o["title"] " by " o["author"] " in r/" o["subreddit"];
	print o["url"];
	print "";
}
$1 == ".data.children[].data" {
	show();
	n++;
	delete o;
}
$1 ~ /^\.data\.children\[\]\.data\.[a-zA-Z0-9_]*$/ {
	o[substr($1, 23)] = $3;
}
END {
	show();
}'


The following awk function can be used to unescape the json2tsv escape
sequences \t, \n and \\:

awk '
BEGIN {
	FS = OFS = "\t";
}
function unescape(s) {
	# use the character "\x01" as a temporary replacement for "\".
	gsub("\\\\\\\\", "\x01", s);
	gsub("\\\\n", "\n", s);
	gsub("\\\\t", "\t", s);
	gsub("\x01", "\\", s); # restore "\x01" to "\".
	return s;
}
$2 == "s" && index($3, "\\") {
	$3 = unescape($3);
}
{
	print $3;
}'

To not have to unescape the data a different field separator and record
separator can be set using the -F and -R option.


License
-------

ISC, see LICENSE file.


Author
------

Hiltjo Posthuma <hiltjo@codemadness.org>
