json2tsv
--------

Convert JSON to TSV.

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


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
--------------------------

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.


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) {
	for (data = ""; (idx = index(s, "\\")); s = substr(s, idx + 2)) {
		prev = substr(s, 1, idx - 1)
		c = substr(s, idx + 1, 1)
		if (c == "t")
			data = data prev "\t"
		else if (c == "n")
			data = data prev "\n"
		else if (c == "\\")
			data = data prev "\\"
		else
			data = data prev # ignore other.
	}
	return data s # rest
}
$2 == "s" && index($3, "\\") {
	$3 = unescape($3);
}
{
	print $3;
}'


License
-------

ISC, see LICENSE file.


Author
------

Hiltjo Posthuma <hiltjo@codemadness.org>
