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

如何使用JQ筛选多层嵌套JSON中的指定元素

用jq实现多层嵌套JSON的精准字段筛选

需求说明

需要对多层嵌套的JSON进行字段筛选,保留整体结构的同时,仅保留以下指定元素:

  • 顶层:image、os、resources、check_summary
  • resources数组中每个对象:仅保留resource(且仅保留其中的name、version)和checks
  • checks数组中每个对象:仅保留name、description字段

已能单独实现部分筛选,但无法组合操作,需要整合后的完整解决方案。

初始JSON结构

{"image": "test","duration": 40,"image_size": 2758644359,"os": "rhel","version": "9.2","resources": [{"resource": {"format": "rpm","name": "binutils","version": "2.35.2-37.el9","src_version": "2.35.2-37.el9"},"installed": true,"checks": [{"name": "check1","description": "first check","severity": "moderate","depth": 5.5},{"description": "second check","severity": "moderate","depth": 4.7},{"name": "check3","description": "third check","severity": "low","depth": 4.2}]},{"resource": {"format": "rpm","name": "dbus","version": "1:1.12.20-7.el9_1","src_version": "1:1.12.20-7.el9_1"},"installed": true,"checks": [{"description": "second check","severity": "moderate","depth": 6.2}]},{"resource": {"format": "rpm","name": "dmidecode","version": "1:3.3-7.el9","src_version": "1:3.3-7.el9"},"installed": true,"checks": [{"name": "check1","description": "first check","severity": "moderate","depth": 5.5},{"description": "second check","severity": "moderate","depth": 7.1}]},{"resource": {"format": "rpm","name": "gcc","version": "11.3.1-4.3.el9","src_version": "11.3.1-4.3.el9"},"installed": true,"checks": []}],"image_assurance_results": {},"check_summary": {"total": 6,"low": 1,"moderate": 5},"initiating_user": "test_user"}

目标JSON结构

{"image": "test","os": "rhel","resources": [{"resource": {"name": "binutils","version": "2.35.2-37.el9"},"checks": [{"name": "check1","description": "first check"},{"description": "second check"},{"name": "check3","description": "third check"}]},{"resource": {"name": "dbus","version": "1:1.12.20-7.el9_1"},"checks": [{"description": "second check"}]},{"resource": {"name": "dmidecode","version": "1:3.3-7.el9"},"checks": [{"name": "check1","description": "first check"},{"description": "second check"}]},{"resource": {"name": "gcc","version": "11.3.1-4.3.el9"},"checks": []}],"check_summary": {"total": 6,"low": 1,"moderate": 5}}

解决方案

使用以下完整的jq命令即可一次性完成所有筛选需求:

jq '{image, os, resources: .resources | map({resource: .resource | {name, version}, checks: .checks | map(with_entries(select(.key | in({"name":1, "description":1}))))}), check_summary}'

命令拆解

  1. 顶层字段筛选:{image, os, resources: ..., check_summary} 直接保留需要的顶层字段,其中resources需单独处理嵌套结构。
  2. resources数组处理:
    • .resources | map(...) 遍历resources数组中的每个元素
    • resource: .resource | {name, version} 提取每个resource对象中的name和version字段
  3. checks数组处理:
    • checks: .checks | map(...) 遍历checks数组中的每个元素
    • with_entries(select(.key | in({"name":1, "description":1}))) 仅保留checks对象中的name和description字段(即使某个对象缺少其中一个字段,也会保留存在的那个)

验证结果

执行上述命令后,输出结果与目标JSON完全一致。

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 10:56:59