mrgreen
mrgreen

Reputation: 1

Newbie question about 'jq' tool for json - how do I create a query for this?

Please help a JSON / jq newbie. I have a JSON-file that looks something like this:

[
   {
        "name": "my_comp_1",
        "desiredVersions": []
    },
    {
        "name": "my_comp_2",
        "desiredVersions": [{
            "name": "1.0",
            "component": {
                "name": "my_comp_2"
            }
        }]
    },
    {
        "name": "my_comp_3",
        "desiredVersions": [{
            "name": "1.1",
            "component": {
                "name": "my_comp_3"
            }
        }]
    }
]

I need to create queries like: What is the version for 'my_comp_2' which in the example should give me '1.0'
I have figured out how to take variables from commandline, but not the jq-query itself. Please help.

Upvotes: 0

Views: 55

Answers (1)

choroba
choroba

Reputation: 242028

One way to pass a value to on the command-line is to use the --arg option. Here, I created the variable $name containing my_comp_2:

$ jq -r --arg name my_comp_2 '
  .[]
  | select(.name == $name)
  | .desiredVersions[].name
' file.json
1.0

-r outputs the "raw" value, without the enclosing double quotes.

Upvotes: 1

Related Questions