0

i'm trying to convert a json to a tab formatted data:

{"level":"INFO", "logger":"db", "msg":"connection successful"}
{"level":"INFO", "logger":"server", "msg":"server started"}
{"level":"INFO", "logger":"server", "msg":"listening on port :4000"}
{"level":"INFO", "logger":"server", "msg":"stopping s ervices ..."}
{"level":"INFO", "logger":"server", "msg":"exiting..."}

to something like this:

INFO   db       connection successful
INFO   server   server started
INFO   server   listening on port 4000
DEBUG  server   stopping s ervices ...
INFO   server   exiting...

I've tried this jq -r ' . | to_entries[] | "\(.value)"', but this prints each value on a separate line.

rezam
  • 627
  • 1
  • 12
  • 17
  • 1
    Possible duplicate of [How to convert arbirtrary simple JSON to CSV using jq?](https://stackoverflow.com/questions/32960857/how-to-convert-arbirtrary-simple-json-to-csv-using-jq) – oliv Oct 22 '18 at 11:40
  • and use `jq ... file | column -t -s, -o$'\t'` to have it tab formatted – oliv Oct 22 '18 at 11:42
  • @oliv, I tried those but didn't work. some give errors. – rezam Oct 22 '18 at 11:50

1 Answers1

1

Assuming the keys are always in the same order, you could get away with:

jq -r '[.[]]|@tsv'

In any case, it would be preferable to use @tsv.

peak
  • 105,803
  • 17
  • 152
  • 177