trubex
trubex

Reputation: 139

How to convert JSON to tsv using jq in unix?

I need to convert this JSON to a TSV format. I've a source file like this:

{
  "event": "log",
  "timestamp": 1535306331840,
  "tags": [
    "info"
  ],
  "data": {
    "_id": "A301180827005852329209020",
    "msisdn": "6282134920902",
    "method": "get",
    "url": "/api/tcash/balance",
    "timeTaken": 32,
    "channelid": "UX"
  },
  "pid": 7920
}

Then I want to convert it to tsv which are consist of below column:

event, timestamp, tags, _id, msisdn, method, url, timeTaken, channelID, pid 

Upvotes: 1

Views: 914

Answers (1)

peak
peak

Reputation: 116870

You just have to construct an array of atomic values. Since .tags is not atomic, in the following I'll assume (as suggested by @chepner) that we can use .tags|join(","), though you might want to use something else, such as .tags|@csv:

[.event, .timestamp, (.tags | join(","))]
+ (.data|[._id, .msisdn, .method, .url, .timeTaken, .channelID])
+ [.pid]
| @tsv 

Upvotes: 1

Related Questions