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

如何使用jq工具实现JSON嵌套字段的筛选保留与删除操作

JSON字段处理需求

待处理原始JSON

[
  {
    "id": "7250078e-5fbf-43d7-9c67-94b88fbf44d8",
    "priority": 0,
    "httpRequest": {
      "method": "POST",
      "path": "/catalog-data-rest/oid/search.xml",
      "headers": {
        "Host": [
          "preproduccio.gcatalegs.isisscat.intranet.gencat.cat:8443"
        ],
        "Authorization": [
          "Basic VVNVTVBJVFNUOkJ4c2w3MjU5"
        ],
        "User-Agent": [
          "curl/7.58.0"
        ],
        "Accept": [
          "*/*"
        ],
        "Content-Type": [
          "text/html; charset=utf-8"
        ],
        "Content-Length": [
          "94"
        ]
      },
      "keepAlive": true,
      "secure": true,
      "body": {
        "type": "STRING",
        "string": "<request><oid>2.16.724.4.402</oid><startIndex>1</startIndex><pageSize>100</pageSize></request>",
        "rawBytes": "PHJlcXVlc3Q+PG9pZD4yLjE2LjcyNC40LjQwMjwvb2lkPjxzdGFydEluZGV4PjE8L3N0YXJ0SW5kZXg+PHBhZ2VTaXplPjEwMDwvcGFnZVNpemU+PC9yZXF1ZXN0Pg==",
        "contentType": "text/html; charset=utf-8"
      }
    },
    "httpResponse": {
      "statusCode": 200,
      "reasonPhrase": "OK",
      "headers": {
        "Date": [
          "Tue, 20 Jul 2021 11:07:40 GMT"
        ],
        "X-Powered-By": [
          "Servlet/3.0"
        ],
        "X-Content-Type-Options": [
          "nosniff"
        ],
        "X-XSS-Protection": [
          "1; mode=block"
        ],
        "Cache-Control": [
          "no-cache, no-store, max-age=0, must-revalidate"
        ],
        "Pragma": [
          "no-cache"
        ],
        "Expires": [
          "0"
        ],
        "Strict-Transport-Security": [
          "max-age=31536000 ; includeSubDomains"
        ],
        "X-Frame-Options": [
          "DENY"
        ],
        "Set-Cookie": [
          "JSESSIONID=0000r6GgpYaB7bQiXRaS8zB8qw8:19pp48tgo; Path=/; HttpOnly"
        ],
        "Keep-Alive": [
          "timeout=10, max=100"
        ],
        "Connection": [
          "Keep-Alive"
        ],
        "Content-Type": [
          "application/xml"
        ],
        "Content-Language": [
          "en-US"
        ],
        "content-length": [
          "29089"
        ]
      },
      "cookies": {
        "JSESSIONID": "0000r6GgpYaB7bQiXRaS8zB8qw8:19pp48tgo"
      },
      "body": {
        "type": "XML",
        "xml": "string-value",
        "rawBytes": "string-value",
        "contentType": "application/xml"
      }
    },
    "times": {
      "remainingTimes": 1
    },
    "timeToLive": {
      "unlimited": true
    }
  }
]

处理规则

httpRequest节点

删除以下字段,其余保留:

  • .httpRequest.headers
  • .httpRequest.keepAlive
  • .httpRequest.secure
  • .httpRequest.body.rawBytes

httpResponse节点

仅保留以下字段,同时删除.body.rawBytes,其余字段全部移除:

  • .httpResponse.statusCode
  • .httpResponse.reasonPhrase
  • .httpResponse.headers.Content-Type
  • .httpResponse.headers.content-length(注:原需求拼写为content-lenght为笔误)

期望最终JSON

[
  {
    "httpRequest": {
      "method": "POST",
      "path": "/catalog-data-rest/oid/search.xml",
      "body": {
        "type": "STRING",
        "string": "<request><oid>2.16.724.4.402</oid><startIndex>1</startIndex><pageSize>100</pageSize></request>",
        "contentType": "text/html; charset=utf-8"
      }
    },
    "httpResponse": {
      "statusCode": 200,
      "reasonPhrase": "OK",
      "headers": {
        "Content-Type": [
          "application/xml"
        ],
        "content-length": [
          "29089"
        ]
      },
      "body": {
        "type": "XML",
        "xml": "string-value",
        "contentType": "application/xml"
      }
    }
  }
]
实现命令

使用如下jq命令即可完成上述转换:

jq 'map({
  httpRequest: .httpRequest | del(.headers, .keepAlive, .secure, .body.rawBytes),
  httpResponse: .httpResponse | {
    statusCode,
    reasonPhrase,
    headers: {
      "Content-Type": .headers."Content-Type",
      "content-length": .headers."content-length"
    },
    body: .body | del(.rawBytes)
  }
})'

命令逻辑说明

  1. 外层用map()遍历数组的每一个元素,只保留需要的httpRequest和httpResponse两个根字段
  2. httpRequest字段直接通过del()方法删除指定的4个不需要的字段,剩余字段自动保留
  3. httpResponse字段采用显式声明的方式重构,仅保留指定的字段,headers只取出需要的两个键,body删除rawBytes字段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 23:03:04