简体   繁体   中英

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 

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM