Using jq how can I replace the name of a key with something else

Use the following jq approach:

jq '[.[] | .["fruit2"] = .veg | .["job"] = .worker | del(.veg, .worker)]' file

The output:

[
  {
    "fruit": "strawberry",
    "fruit2": "apple",
    "job": "gardener"
  }
]

The key (:-) is with_entries. E.g., given a single object:

with_entries(if .key == "veg" then .key = "fruit2" else . end)

In your case, since you have an array of objects, you could wrap the above in map( ... ).

Tags:

Key

Json

Jq