0

jq key assignment is not working.

I tried what was here for my use case. Using jq how can I replace the name of a key with something else

in this case I want to change id to item_id`.

jq '.item[]  | select(.closed == false) | select(.id == "1234") |  .["id"] = .["item_id"] | .' data.json | less

the result does not error but "id" gets a value of null and there is not key called item_id. so there is something I am not getting right about assignment of the key.

user3738936
  • 936
  • 8
  • 22

1 Answers1

1

You can just delete the old field using del, and add another one using object construction:

jq '.item[]  | select(.closed == false and .id == "1234") | del(.id) + {item_id: .id}' data.json

Or use with_entries to "rename" the old field:

jq '.item[]  | select(.closed == false and .id == "1234") | with_entries(select(.key == "id").key = "item_id")' data.json
pmf
  • 24,478
  • 2
  • 22
  • 31