Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

csv - Nested JSON with variable keys to TSV using jq

I have the following nested JSON file labs.json with variable keywords (lab001, lab002, etc.) which I would like to convert into a TSV using jq:

{
  "lab001": {
    "tags": {
      "T1": [],
      "T2": ["k26","e23"],
      "T3": ["s92"]
    },
    "code": "8231"
  },
  "lab002": {
    "tags": {
      "T1": ["t32","y55"],
      "T2": ["q78"],
      "T3": ["b24"]
    },
    "code": "9112"
  }
}

The resulting table should look like:

ID T1 T2 T3
lab001 k26,e23 s92
lab002 t32,y55 q78 b24
question from:https://stackoverflow.com/questions/65831692/nested-json-with-variable-keys-to-tsv-using-jq

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Join elements of each tag by commas, put resulting strings into an array with the lab ID as the first element, and pipe it to the @tsv filter like so:

keys_unsorted[] as $id | [$id, (.[$id].tags[] | join(","))] | @tsv

Online demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...