{"version":"https://jsonfeed.org/version/1.1","title":"Vic Demuzere - jq","home_page_url":"https://vic.demuzere.be/articles/tags/jq/","feed_url":"https://vic.demuzere.be/articles/tags/jq/feed.json","description":"Blog posts tagged jq by Vic Demuzere","language":"en","items":[{"id":"https://vic.demuzere.be/articles/using-bash-variables-in-jq/","content_html":"<p>Due to the special characters used in json, the easiest way to use jq with inline scripts it by putting it between single quotes.\nThat <strong>makes it impossible to use bash variables inside your script</strong>.\nFortunately, jq has an <code>--arg</code> parameter to create a predefined variable from an external source.</p>\n<p>You can use it to define a <code>$foo</code> variable with the contents of bash variable <code>$FOO</code>, for example:</p>\n<pre><code>$ export FOO=&quot;bar&quot;\n$ jq -n --arg foo &quot;$FOO&quot; '{foo: $foo}'\n{\n  &quot;foo&quot;: &quot;bar&quot;\n}\n</code></pre>\n<p>You might notice the <code>-n</code> parameter: This tells <code>jq</code> to use <code>null</code> as input instead of reading json from stdin like it normally would.\nI'm using it in all examples on this page as it makes them a bit shorter.\nIn real world usage, you probably won't need it.</p>\n<p>As usual in bash scripts, you can also execute a command inline:</p>\n<pre><code>$ jq -n --arg hostname &quot;$(hostname)&quot; '{foo: $hostname}'\n{\n  &quot;foo&quot;: &quot;cypher&quot;\n}\n</code></pre>\n<p>Every variable created with <code>--arg</code> is <strong>treated as a string</strong>.\nThat might cause some unexpected behavior when you work with numbers, like when you attempt to add a number to an argument.\nComparisons between strings and numbers seem to work fine, but I wouldn't count on it (pun intended).</p>\n<pre><code>$ echo '{&quot;a&quot;: 3.0}' | jq --arg foo 5 '$foo + .a'\njq: error (at &lt;stdin&gt;:1): string (&quot;5&quot;) and number (3) cannot be added\n</code></pre>\n<p>There are two ways to solve this: either convert the variable to a number with <code>tonumber</code> or use <code>--argjson</code> instead.\nI prefer to use <code>tonumber</code> because that way I'm sure jq forces it to be a number.\nAs my argument may be the result of an earlier command, I may be using an error message as input instead of the expected numeric result.</p>\n<pre><code>$ jq -n --arg foo 5 '($foo|tonumber) + 1'\n6\n\n$ jq -n --argjson foo 5 '$foo + 1'\n6\n</code></pre>\n<h2 id=\"variables-from-files\">Variables from files<a href=\"#variables-from-files\" aria-label=\"Link to heading 'Variables from files'\" data-heading-content=\"Variables from files\" class=\"anchor\"></a></h2>\n<p>It's also possible to load variables from files.\nWith <code>--slurpfile</code> we can read a file containing json objects.\nAn array containing the separate objects is then made available as a predefined variable.\nNote that even if your file contains only a single json object, as the variable is still an array, the <code>[0]</code> is required to access it.</p>\n<pre><code>$ cat foo.json\n{&quot;foo&quot;: &quot;bar&quot;}\n{&quot;bar&quot;: &quot;foo&quot;}\n\n$ jq -n --slurpfile foo foo.json '$foo[0].foo'\n&quot;bar&quot;\n</code></pre>\n<p>A second option is <code>--rawfile</code>, creating a string variable with the exact contents of the file.\nNote that as jq outputs json, reading and printing the file from the previous example results an escaped string.</p>\n<pre><code>$ jq -n --rawfile foo foo.json '$foo'\n&quot;{\\&quot;foo\\&quot;: \\&quot;bar\\&quot;}\\n{\\&quot;bar\\&quot;: \\&quot;foo\\&quot;}\\n&quot;\n</code></pre>\n<p>That's it!</p>\n<div class=\"footer\"><hr><p>Thanks for subscribing to my feed!</p><p>Feel free to e-mail comments or corrections to <a href=\"mailto:vic@demuzere.be?subject=RE%3A%20Using%20bash%20variables%20in%20jq\">vic@demuzere.be</a>.</p></div>","url":"https://vic.demuzere.be/articles/using-bash-variables-in-jq/","date_published":"2021-02-10T20:01:03Z","date_modified":"2024-11-11T18:21:44Z","language":"en"}]}