Home > Software design >  Convert json to long format table using jq
Convert json to long format table using jq

Time:12-10

Given an array of JSON objects, all having the same key names (key1, key2, key3) and just one key (key3) whose value is an array, how can it be converted to a long format table?

Input:

[
  { "key1": "A",
    "key2": 1,
    "key3" : ["aaa", "bbb"]
  },
  { "key1": "B",
    "key2": 2,
    "key3" : ["ccc", "ddd"]
  }
]

Desired output:

key1 key2 key3
A 1 aaa
A 1 bbb
B 2 ccc
B 2 ddd

CodePudding user response:

.[]| ([.key1,.key2]   (.key3[]|[.])) | @csv
  • Related