Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use jq to remove key/value pairs based on other file

I have 2 json files containing translations for my app:

en-US.json:

{
    "car": "car",
    "bike": "bike",
    "tree": "tree",
}

nl-NL.json:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "car": "auto",
    "bike": "fiets",
    "tree": "boom",
    "house": "huis"
}

As you can see, I have removed the house from the en-US.json file (among many others). How could I remove the same house entry from my nl-NL.json file with jq? I basically want to get the intersection of both files, based on key.

I’ve been playing with jq 'keys' to get all keys, but that does not work. I think it should be found in the direction of jq --slurpfile en en-US.json 'del($en)' nl-NL.json but that totally does not work 🙁 Any suggestions?

>Solution :

To reduce the nl-NL.json file to the keys present in en-US.json, you could read in the latter as reference, then select from the input turned into entries those keys that are present in the reference file. in checks "whether or not the input key is in the given object".

jq --argfile ref en-US.json 'with_entries(select(.key | in($ref)))' nl-NL.json
{
  "car": "auto",
  "bike": "fiets",
  "tree": "boom"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading