Skip to content

how to rename key in a map (or hash) #898

Answered by mikefarah
hungpr0 asked this question in Q&A
Discussion options

You must be logged in to vote

Your example is a little more complex than that, as you have an array of maps as opposed to a single one. There's also a new with_entries operator that wasn't around for #741.

yq e '.users.[] |= with_entries( (select(.key=="name") | .key) ="username")'  file.yml

Explanation:

  • we want to update each map in the array, relative to itself, so we do .users[] |=
  • Use with_entries, this will turn the map into an array of {key, value} pairs. We select the key,value pair we want to update, and set it's new key value.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@hungpr0
Comment options

Answer selected by hungpr0
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants