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

如何用jq向httpie会话文件的headers子键插入值并输出完整JSON

How to Add a New Header Entry to the Full JSON Object Using jq

Got it, I see exactly what's going on here—your current jq command only returns the updated .headers sub-object instead of the full JSON structure you need. To fix this, you need to modify the .headers field in-place within the original parent object, rather than just returning the merged headers alone.

The Correct jq Command

Use this command to keep the full JSON output while adding your new Cookie header:

jq '.headers += {"Cookie": "xyz"}'

Why This Works

  • The += operator tells jq to update the existing .headers field by merging the new key-value pair into it, instead of creating a standalone merged headers object.
  • This preserves the entire original JSON structure, with the new Cookie entry added directly to the headers section.

Example Walkthrough

Input JSON

{ "headers": { "Host": "34.213.0.202", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "DNT": "1", "Connection": "keep-alive", "Upgrade-Insecure-Requests": "1", "Cache-Control": "max-age=0" } }

Output After Running the Command

{
  "headers": {
    "Host": "34.213.0.202",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate",
    "DNT": "1",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Cache-Control": "max-age=0",
    "Cookie": "xyz"
  }
}

Alternative Explicit Syntax

If you prefer a more verbose (but equally valid) approach, you can reconstruct the full object explicitly:

jq '. | .headers = .headers + {"Cookie": "xyz"}'

This achieves the same result, but the += shorthand is cleaner for this specific use case.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:03:53