jsonは特定の値(サブオブジェクトごとに1行)のみを印刷し、各行の最上位オブジェクトで1つの値を繰り返そうとします。
入力例:
[
{
"name": "level1Name",
"lv1id": "id1",
"requests": [
{
"lv2id": "id2",
"name": "lvl2requestA",
"startDate": "2019-02-05 08:52:33.663+0000",
"requestState": "Succeeded"
},
{
"lv2id": "id2",
"name": "lvl2requestB",
"startDate": "2019-02-05 08:52:33.421+0000",
"requestState": "Succeeded"
}
]
},
{
"name": "level1Name",
"lv1id": "id12",
"requests": [
{
"lv2id": "id2",
"name": "lvl2requestD",
"startDate": "2019-02-05 08:52:19.823+0000",
"requestState": "Succeeded"
},
{
"lv2id": "id2",
"name": "lvl2requestA",
"startDate": "2019-02-05 08:52:19.689+0000",
"requestState": "Succeeded"
}
]
}
]
目標出力
["id1","2019-02-05 08:52:33.663+0000","lvl2requestA","Succeeded"]
["id1","2019-02-05 08:52:33.421+0000","lvl2requestB","Succeeded"]
["id12","2019-02-05 08:52:19.823+0000","lvl2requestD","Succeeded"]
["id12","2019-02-05 08:52:19.689+0000","lvl2requestA","Succeeded"]
何度も試しました(例:変数/記号バインディング演算子を使用)。すべてのリードを歓迎します。
答え1
$ jq -c '.[] | .lv1id as $id | .requests[] | [$id, .startDate, .name, .requestState]' file.json
["id1","2019-02-05 08:52:33.663+0000","lvl2requestA","Succeeded"]
["id1","2019-02-05 08:52:33.421+0000","lvl2requestB","Succeeded"]
["id12","2019-02-05 08:52:19.823+0000","lvl2requestD","Succeeded"]
["id12","2019-02-05 08:52:19.689+0000","lvl2requestA","Succeeded"]
これは最上位配列(.[]
)の各要素を取得し、その要素.lv1id
をに割り当てます$id
。次に、対応.requests[]
する要素の配列を繰り返し、各要素の出力配列を構成します。それ$id
以前に覚えていた部分を含め、必要に見えるさまざまな部分で構成される要素です。