micha/json-table

inconsistent formatting

Closed this issue · 2 comments

echo '[{"foo": "asdf-qwer-asdf-qwer-asdf", "bar": "poiu"},{"foo":"asdf","bar":"kjkjkj"}]' |\
  jt [ foo %=foo ] [ bar %=bar ]

produces the following misaligned table that's difficult to read:

foo     bar
asdf-qwer-asdf-qwer-asdf        poiu
asdf    kjkjkj

whereas

echo '[{"foo": "asdf-qwer-asdf-qwer-asdf", "bar": "poiu"},{"foo":"asdf","bar":"kjkjkj"}]' |\
  jt [ foo %=foo ] [ bar %=bar ] |\
  column -ts $'\t'

produces the desired and aligned output:

foo                       bar
asdf-qwer-asdf-qwer-asdf  poiu
asdf                      kjkjkj
micha commented

The output is TSV (tab separated values) by default. This is because JSON values may contain spaces, and it would be impossible to split columns if they were padded with spaces. For example:

echo '[{"foo":"one two","bar":200},{"foo":"three four","bar":400}]' \
  |jt [ foo % ] [ bar % ] \
  |awk -F\\t '$1 == "one two" {print}'

The -F\\t option to awk sets the input field separator to the tab character, which makes it possible to dispatch on individual columns of the output.

Since jt is intended to be use in shell pipelines, as plumbing, it's okay that the user chooses the display format he prefers (like the column -ts $'\t' in your example).

micha commented

Feel free to reopen if necessary.