README - json2tsv - JSON to TSV converter
 (HTM) git clone git://git.codemadness.org/json2tsv
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       README (2121B)
       ---
            1 json2tsv
            2 --------
            3 
            4 Convert JSON to TSV or separated output.
            5 
            6 json2tsv reads JSON data from stdin.  It outputs each JSON type to a TAB-
            7 Separated Value format per line by default.
            8 
            9 
           10 Build and install
           11 -----------------
           12 
           13 $ make
           14 # make install
           15 
           16 
           17 Dependencies
           18 ------------
           19 
           20 - C compiler (C99).
           21 - libc
           22 
           23 
           24 Optional dependencies
           25 ---------------------
           26 
           27 - POSIX make(1) (for Makefile).
           28 - mandoc for documentation: https://mdocml.bsd.lv/
           29 
           30 
           31 TAB-Separated Value format
           32 --------------------------
           33 
           34 By default the output format per line is:
           35 
           36         nodename<TAB>type<TAB>value<LF>
           37 
           38 The nodename and value are escaped (\n, \t and \\).  Control-characters are
           39 removed.
           40 
           41 The type field is a single byte and can be:
           42 
           43         a for array
           44         b for bool
           45         n for number
           46         o for object
           47         s for string
           48         ? for null
           49 
           50 Filtering on the first field "nodename" is easy using awk for example.
           51 
           52 See the json2tsv(1) man page for the full documentation of the options.
           53 
           54 
           55 Example
           56 -------
           57 
           58 An usage example to parse JSON posts of reddit.com and format them to a
           59 plain-text list using awk:
           60 
           61 
           62 #!/bin/sh
           63 curl -s -H 'User-Agent:' 'https://old.reddit.com/.json?raw_json=1&limit=100' | \
           64 json2tsv | \
           65 awk -F '\t' '
           66 function show() {
           67         if (length(o["title"]) == 0)
           68                 return;
           69         print n ". " o["title"] " by " o["author"] " in r/" o["subreddit"];
           70         print o["url"];
           71         print "";
           72 }
           73 $1 == ".data.children[].data" {
           74         show();
           75         n++;
           76         delete o;
           77 }
           78 $1 ~ /^\.data\.children\[\]\.data\.[a-zA-Z0-9_]*$/ {
           79         o[substr($1, 23)] = $3;
           80 }
           81 END {
           82         show();
           83 }'
           84 
           85 
           86 The following awk function can be used to unescape the json2tsv escape
           87 sequences \t, \n and \\:
           88 
           89 awk '
           90 BEGIN {
           91         FS = OFS = "\t";
           92 }
           93 function unescape(s) {
           94         # use the character "\x01" as a temporary replacement for "\".
           95         gsub("\\\\\\\\", "\x01", s);
           96         gsub("\\\\n", "\n", s);
           97         gsub("\\\\t", "\t", s);
           98         gsub("\x01", "\\", s); # restore "\x01" to "\".
           99         return s;
          100 }
          101 $2 == "s" && index($3, "\\") {
          102         $3 = unescape($3);
          103 }
          104 {
          105         print $3;
          106 }'
          107 
          108 To not have to unescape the data a different field separator and record
          109 separator can be set using the -F and -R option.
          110 
          111 
          112 License
          113 -------
          114 
          115 ISC, see LICENSE file.
          116 
          117 
          118 Author
          119 ------
          120 
          121 Hiltjo Posthuma <hiltjo@codemadness.org>