170730350
170730350

Reputation: 622

jq - insert values into subkey

I have a httpie session file that looks like this:

{
   "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"
    }
}

I would like to insert an item into headers (think '.headers + {"Cookie": "xyz"}') but I would like the output to be the whole JSON. Using jq '.headers + {"Cookie": "xyz"}' only prints out the .headers value. What I am looking to get is the whole entire JSON, but with the cookie subkey included.

What's the way to get that?

Upvotes: 1

Views: 331

Answers (1)

oguz ismail
oguz ismail

Reputation: 50795

Use assignment operators:

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

The latter one changes a bit when there are multiple fields to be added:

.headers |= (.Cookies = "xyz" | .Referer = "xyz")

Upvotes: 1

Related Questions