0

I have a bash function like below.

function createJson
{
START=1
END=100    
jq -n 'reduce range($START; $END) as $data (.; . + [{"field": { "empID": $data, "location": "India"}}])'  > test.json    
}

The above script works fine If I mention the direct numeric value in "reduce range". For Example:

jq -n 'reduce range(0; 100) as $data (.; . + [{"field": { "empID": $data, "location": "India"}}])'  > test.json

But when I tried to pass the shell variable to jq -n reduce range it throws error like below.

jq: error: START/0 is not defined at <top-level>, line 1:
reduce range($START; $END) as $data (.; . + [{"field": { "empID": $data, "location": "India"}}])
jq: error: END/0 is not defined at <top-level>, line 1:
reduce range($START; $END) as $data (.; . + [{"field": { "empID": $data, "location": "India"}}])
jq: 2 compile errors
oguz ismail
  • 1
  • 16
  • 47
  • 69
Mario Super
  • 309
  • 3
  • 14

1 Answers1

3

START and END are not visible to JQ as they're shell variables. You need to pass them to JQ as arguments or environment variables.

jq -n --argjson START 1 --argjson END 100 'reduce range($START; $END) as $data (.; . + [{"field": { "empID": $data, "location": "India"}}])' > test.json
oguz ismail
  • 1
  • 16
  • 47
  • 69