Jq: Pass multiple arguments into jq statement

Created on 28 Oct 2014  路  4Comments  路  Source: stedolan/jq

Would like to be able to pass multiple arguments into a jq statement like so:

jq --arg ARG1 ${var1} ARG2 ${var2} '.$ARG1[$ARG2 | tonumber].width'

support

Most helpful comment

You're right, the quotes didn't end up working, but the brackets did.

jq --arg ARG1 ${var1} --arg ARG2 ${var2} '.[$ARG1][$ARG2 | tonumber].width'

All 4 comments

@rclod - You can pass in as many named arguments as you like by repeating the "--arg NAME VALUE" pattern.

thanks! I had tried that earlier, but had forgotten to put quotes around my variable call.

This ended up working as expected:

jq --arg ARG1 ${var1} --arg ARG2 ${var2} '."$ARG1"[$ARG2 | tonumber].width'

@rclod - Because of variations between different versions of jq, it's sometimes simpler just to use the form .[s] instead of .s. Or in your case: .[$ARG1][$ARG2]

You're right, the quotes didn't end up working, but the brackets did.

jq --arg ARG1 ${var1} --arg ARG2 ${var2} '.[$ARG1][$ARG2 | tonumber].width'

Was this page helpful?
0 / 5 - 0 ratings