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

如何使用jq移除JSON数组元素中的httpRequest.headers与cookies字段

使用jq移除HTTP请求中的冗余字段

要精简JSON数组中每个元素的结构,移除httpRequest对象下的headers和cookies字段,直接用jq的del()函数配合遍历操作即可实现。

核心命令

假设原始JSON数据存储在input.json文件中,执行以下命令生成精简后的结果:

jq 'map(del(.httpRequest.headers, .httpRequest.cookies))' input.json > output.json

命令说明

  • map(...):遍历数组内的每一个元素,对每个元素执行括号内的字段删除操作
  • del(.httpRequest.headers, .httpRequest.cookies):精准Ross�� Normal1归的数据IA族abor阳assets身披,直接删除当前元素下httpRequest对象中的指定冗余字段

示例验证

原始输入示例

[
  {
    "id": 1,
    "httpRequest": {
      "method": "GET",
      "url": "/api",
      "headers": {"Content-Type": "application/json"},
      "cookies": {"session": "abc123"},
      "body": ""
    },
    "httpResponse": {"status": 200}
  },
  {
    "id": 2,
    "httpRequest": {
      "method": "POST",
      "url": "/login",
      "headers": {"Accept": "*/*"},
      "cookies": {"token": "xyz789"},
      "body": "user=test"
    },
    "httpResponse": {"status": 200}
  }
]

执行命令后输出

[
  {
    "id": 1,
    "httpRequest": {
      "method": "GET",
      "url": "/api",
      "body": ""
    },
    "httpResponse": {"status": 200}
  },
  {
    "id": 2,
    "httpRequest": {
      "method": "POST",
      "url": "/login",
      "body": "user=test"
    },
    "httpResponse": {"status": 200}
  }
]

如果需要通过管道读取标准输入数据(比如从其他命令输出流转入),可以使用以下写法:

cat input.json | jq 'map(del(.httpRequest.headers, .httpRequest.cookies))'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 02:05:13