You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jq从内部数组按条件取值并移至数组外部的实现方法

Extract Tag Values from Assets' Tags Array to Top-Level Fields with 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 creating
  • mutable_tag_values: [.tags[] | select(.mutable == true) | .value]: Filter tags to only those where mutable is true, extract their value, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:34:59