Skip to content
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

Closed
desimaniac opened this issue Feb 10, 2019 · 7 comments
Closed

Question: Is there a way to rename keys? #210

desimaniac opened this issue Feb 10, 2019 · 7 comments

Comments

@desimaniac
Copy link

desimaniac commented Feb 10, 2019

Hi. Had a question.

Say you have a sample.yaml file with :

b:
  c: cat
  d: dog

Could you rename b to y?

y:
  c: cat
  d: dog
@mikefarah
Copy link
Owner

hmm not atm :(

Even if you use multiple commands, the 'write' command will put the value in as a string

@ipfaffy
Copy link

ipfaffy commented Mar 4, 2019

+1 was just looking for a way to do this. Would be a great addition.

@ideasasylum
Copy link

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}`

@mikefarah
Copy link
Owner

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.

@mfulgo
Copy link

mfulgo commented Mar 30, 2022

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

@LarsBingBong
Copy link

@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 🙇🏿

@mikefarah
Copy link
Owner

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:

yq '.nested |= with_entries(.key |= "KEY_" + .)' data1.yaml

or; if you need to use with:

yq 'with(.nested; . |= with_entries(.key |= "KEY_" + .))' data1.yaml

This also follows with what I documented for with: https://mikefarah.gitbook.io/yq/operators/with

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants