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

jq how to use split with nested keys in single –arg

I would like to pass a nested key to jq in an arg. Using a single --arg key $mypath fails.

Looking at other questions it seems split is the correct method, but I cant find a basic example that works.

mypath="level1.level2.level3"
//working hardcode
jq '.level1.level2.level3' $myfile

//variations tried
jq --arg nest "${mypath}" '.[$nest]' $myfile
jq --arg nest "${mypath}" '.[$nest]|split(".")' $myfile
jq --arg nest "${mypath}" '.|split(".")|[$nest]' $myfile

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

>Solution :

You need getpath/1 for this. The method takes an array of paths to a particular node and returns the value present at it

jq --arg nest "$mypath" 'getpath($nest|split("."))'
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