使用jq从内部数组按条件取值并移至数组外部的实现方法
Got it, let's break down how to use jq to pull specific tag values from the nested tags array inside each assets element and attach them as top-level fields to the asset itself. I'll cover a couple common scenarios based on your sample JSON.
Sample Input JSON
First, let's restate your input for reference:
{ "data": { "assets": [ { "aDASD": "1056bda9-2598-4fdf-bd99-db3924464a75", "KEY": "a7e2ef79-41aa-4a36-9ca1-1f388917eb12.ahnl", "key2": "store-order-picking-units-api", "key3": "1.0.1", "tags": [ { "value": "abcd", "key": null, "mutable": false }, { "value": "rest", "key": null, "mutable": false }, { "value": "123", "key": null, "mutable": false }, { "value": "system", "key": null, "mutable": true }, { "value": "market", "key": null, "mutable": true } ], "type": "mytype" } ] } }
Scenario 1: Extract all tags where mutable: true into a new array field
If you want to collect all tag values that have mutable: true and add them as a mutable_tag_values field to each asset, use this jq command:
.data.assets[] |= ( . + { mutable_tag_values: [.tags[] | select(.mutable == true) | .value] } ) | { data: { assets: [.data.assets[]] } }
Explanation:
.data.assets[] |=: Iterate over each asset element and modify it in-place. + { ... }: Merge the original asset with a new field we're creatingmutable_tag_values: [.tags[] | select(.mutable == true) | .value]: Filter tags to only those wheremutableis true, extract theirvalue, and wrap the results in an array| { data: { assets: [.data.assets[]] } }: Reconstruct the original JSON structure after modifications
Output for this scenario:
{ "data": { "assets": [ { "aDASD": "1056bda9-2598-4fdf-bd99-db3924464a75", "KEY": "a7e2ef79-41aa-4a36-9ca1-1f388917eb12.ahnl", "key2": "store-order-picking-units-api", "key3": "1.0.1", "tags": [/* original tags array */], "type": "mytype", "mutable_tag_values": ["system", "market"] } ] } }
Scenario 2: Extract a specific tag value (e.g., where value == "system") as a single field
If you only need a specific tag value (like "system") as a top-level field, use this:
.data.assets[] |= ( . + { system_tag: .tags[] | select(.value == "system") | .value } ) | { data: { assets: [.data.assets[]] } }
Output for this scenario:
{ "data": { "assets": [ { "aDASD": "1056bda9-2598-4fdf-bd99-db3924464a75", "KEY": "a7e2ef79-41aa-4a36-9ca1-1f388917eb12.ahnl", "key2": "store-order-picking-units-api", "key3": "1.0.1", "tags": [/* original tags array */], "type": "mytype", "system_tag": "system" } ] } }
Scenario 3: Extract multiple specific tags into individual fields
If you want to pull multiple specific tags into their own fields (e.g., "rest" and "market"), you can extend the approach:
.data.assets[] |= ( . + { rest_tag: (.tags[] | select(.value == "rest") | .value), market_tag: (.tags[] | select(.value == "market") | .value) } ) | { data: { assets: [.data.assets[]] } }
Pro Tip:
If you want to remove the original tags array after extracting the values, just add del(.tags) inside the modification block:
.data.assets[] |= ( . + { mutable_tag_values: [.tags[] | select(.mutable == true) | .value] } | del(.tags) ) | { data: { assets: [.data.assets[]] } }
内容的提问来源于stack exchange,提问作者Naveen K Reddy

