cannot call bash environment variable inside jq

Related searches

In the below script, I am not able to successfully call the "repovar" variable in the jq command.

cat quayrepo.txt | while read line
do
  export repovar="$line"
  jq -r --arg repovar "$repovar" '.data.Layer| .Features[] | "\(.Name), \(.Version), $repovar"' severity.json > volume.csv
done

The script uses a text file to loop through the repo names

quayrepo.txt---> file has the list of names in this case the file has a value of "Reponame1"

sample input severity.json file:

{
  "status": "scanned",
  "data": {
    "Layer": {
      "IndexedByVersion": 3,
      "Features": [
        {
          "Name": "elfutils",
          "Version": "0.168-1",
          "Vulnerabilities": [
            {
              "NamespaceName": "debian:9",
              "Severity": "Medium",
              "Name": "CVE-2016-2779"
            }
          ]
        }
      ]
    }
  }
}

desired output:

elfutils, 0.168-1, Medium, Reponame1

Required output: I need to retrieve the value of my environment variable as the last column in my output csv file

You need to surround $repovar with parenthesis, as the other values

repovar='qweqe'; jq -r --arg repovar "$repovar" '.data.Layer| .Features[] | "\(.Name), \(.Version), \($repovar)"' tmp.json

Result:

elfutils, 0.168-1, qweqe

There's no need for the export.

Accessing the environment from jq � Issue #405 � stedolan/jq � GitHub, In the below script, I am not able to successfully call the "repovar" variable in the jq command. cat quayrepo.txt | while read line do export� In any case, even in zsh or bash which support that operator, that code doesn't make any sense. It redirects the stdin of echo which doesn't read it. It's a wonder how that answer managed to get 10 upvotes.

#!/usr/bin/env bash

while read line
do
  jq -r --arg repovar "$line" '.data.Layer.Features[] | .Name + ", " + .Version + ", " + $repovar' severity.json
done < quayrepo.txt > volume.csv

with quayrepo.txt as

Reponame1

and severity.json as

{
  "status": "scanned",
  "data": {
    "Layer": {
      "IndexedByVersion": 3,
      "Features": [
        {
          "Name": "elfutils",
          "Version": "0.168-1",
          "Vulnerabilities": [
            {
              "NamespaceName": "debian:9",
              "Severity": "Medium",
              "Name": "CVE-2016-2779"
            }
          ]
        }
      ]
    }
  }
}

produces volume.csv containing

elfutils, 0.168-1, Reponame1

To @peak's point, changing > to >> in ...severity.json >> volume.csv will create a multi-line csv instead of just overwriting until the last line

Unable to pass args to jq in shell script � Issue #1933 � stedolan/jq , Is there anyway to use variables within the JQ commands that have been (and can't) mean that jq can't do I/O (it might mean that jq can't execute title Using JQ with variables in bash Accessing the environment from jq on� Using JSON Array Variables in jq. Using the --slurp option you can store all JSON input records into an array. Using the --slurpfile option you can store all JSON records from a file into an array, and reference this array using a variable.

Setting jq output to a Bash Variable, I need to write a shell script in BASH that allows me to call variety of URLs and extract the keys from the JSON that is returned. I have been� Environment Variables. Bash uses environment variables to define and record the properties of the environment it creates when it launches. These hold information Bash can readily access, such as your username, locale, the number of commands your history file can hold, your default editor, and lots more.

JSON array to bash variables using jq, Changed the code to this and it worked: content=$(curl -s -X GET -H "Header: Value" http://127.0.0.1:8200/etc) username=$( echo jq -r '.data.value'� (Optional) If this is true, the task will not process .bashrc from the user's home directory. Default value: true: env Environment variables (Optional) A list of additional items to map into the process's environment. For example, secret variables are not automatically mapped. If you have a secret variable called Foo, you can map it in like this:

deals with taking each object in the array through the rest of the pipeline as a separate At the end, we just assemble a valid shell assignment string with ordinary + way to avoid or multiple calls to jq to set a value per variable or even to using some can be valid and empty, leading to a value shift (that was my problem). Each alternative need not define all of the same variables, but all named variables will be available to the subsequent expression. Variables not matched in the alternative that succeeded will be null:

jq Manual (development version) For released versions, see jq 1.6, jq 1.5, jq 1.4 or jq 1.3.. A jq program is a "filter": it takes an input, and produces an output. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks.

Comments
  • Please include sample input and desired output.
  • You probably want >> instead of > as the latter will overwrite whatever was written before.
  • @vintnes- made the edits... let me know if you need any further info
  • while we're nitpicking: while read line; do ...; done < quayrepo.txt
  • Heck, while read -r repovar; do -- why use a line variable at all?
  • Better to just put > volume.csv after the done, not on the individual jq line. That way each jq call appends, but the loop as a whole clears any prior content when starting -- and you have (ever-so-slightly) better performance since you aren't re-opening the output file over and over, as you do with >> volume.csv on the individual jq.