0

here i am parsing a json file and storing values as array.

arr1=(`jq '.animal.names'  123.json | sed...`)

output: fox tiger lion

now i need to parse values dynamically (i = birds or fish)

eg    arr1=(`jq .$i.names  123.json | sed...`)
it should be as 

arr1=(`jq '.bird.names'  123.json | sed...`) 
or      
arr1=(`jq '.fish.names'  123.json | sed...`).

Some guidance will be helpful.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
mebb
  • 123
  • 10
  • So, your question is not about jq output and bash arrays, but how to pass a bash variable to a jq command, which is well documented around SO, like here: https://stackoverflow.com/questions/40027395/passing-bash-variable-to-jq – thanasisp Oct 04 '20 at 19:24
  • 2
    If you mean something more than just passing a variable, please update your question with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with exact commands, some small input and the expected output. – thanasisp Oct 04 '20 at 20:11
  • 2
    Does this answer your question? [Passing bash variable to jq](https://stackoverflow.com/questions/40027395/passing-bash-variable-to-jq) – Hedi Bejaoui Oct 04 '20 at 20:47

1 Answers1

0

I'm not entirely sure what your question really is, because that syntax should already work. The only thing missing is to actually declare and assign the variable:

i=bird
arr1=(`jq ".$i.names" 123.json | sed...`)

Or by defining a variable for jq and using it (same concept):

i=bird
arr1=(`jq --arg animal "$i" '.[$animal].names' 123.json | sed...`)
knittl
  • 246,190
  • 53
  • 318
  • 364