-
-
Notifications
You must be signed in to change notification settings - Fork 564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Is there a way to rename keys? #210
Comments
hmm not atm :( Even if you use multiple commands, the 'write' command will put the value in as a string |
+1 was just looking for a way to do this. Would be a great addition. |
fwiw, I did this by reading the keys I want to rename into a tmp file, prefixing them with the new key name, deleting the old keys, and then merging the new keys with the everything-but-old-keys. In Ruby it looks like this (with some parameterisation) # Extract the old keys we want to move
`yq r #{filename} en.#{old_key} > en.extract.yml`
# Prefix the extracted keys with the new key
`yq p en.extract.yml en.#{new_key} > en.prefixed.yml`
# Delete the old keys
`yq d config/locales/en.yml en.#{old_key} > en.deleted.yml`
# Merge the new keys into the file without the old keys
`yq m en.deleted.yml en.prefixed.yml > #{filename}` |
This is now possible in v4 of yq, create a new object with the desired key, merge it into the existing object, then delete the old key. |
I landed on this issue while trying to figure out how to prefix keys in v4. Just adding it as a comment here in case anyone else lands here with the same issue. a:
b:
c: charlie
d: delta
e: other yq 'with(.a.b; with_entries(.key |= "prefix_" + .))' $FILE Should produce a:
b:
prefix_c: charlie
prefix_d: delta
e: other |
@mfulgo you saved my day - thanks! Now I just need to dive into exactly what's going on in that cmd. If you care and have 5 - I would love you to give it some words 🙇🏿 |
Update: Upon closer inspection the example above isn't quite right. It's not assigning the with_entries result to anything. Not sure how it was working before - probably a bug 😓 It should be either:
or; if you need to use
This also follows with what I documented for |
Hi. Had a question.
Say you have a sample.yaml file with :
Could you rename
b
toy
?The text was updated successfully, but these errors were encountered: