title: Sending concatenated files to JQ with wrapped as JSON array
body: <p>I have a curious problem sending concatenated files to <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer">JQ</a>. I'm using Windows 10, but the problem is surfacing with Bash (via Git Bash) as well.</p>
  <p>I have several input JSON files in the form <code>input-1.json</code>, <code>input-2.json</code>, <code>input-3.json</code>, etc. The contents of each is a nested object that looks like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;blah&quot;: &quot;blah&quot;,
    &quot;foo&quot;: [{
        &quot;bar&quot; {…}
      }, {
        &quot;bar&quot; {…}
      }]
  }
  </code></pre>
  <p>I want to concatenate the files and extract all the values of &quot;bar&quot; across all the files into one array. (Note that &quot;foo&quot; is an array of objects, each with &quot;bar&quot; containing its own object.) In other words, I want to end up with (placed on one line for readability here):</p>
  <pre class="lang-json prettyprint-override"><code>[{…}, {…}, {…}, {…}, {…}]
  </code></pre>
  <p>Note that these are the foo…bars <em>across all the files</em>!</p>
  <p>I start with this:</p>
  <pre><code>type input-*.json | jq &quot;.foo[].bar&quot; &gt; output.json
  </code></pre>
  <p>That gives me</p>
  <pre><code>{…}, {…}, {…}, {…}, {…}
  </code></pre>
  <p>Close! Now I just have to wrap them in <code>[…]</code>, right? So I do this:</p>
  <pre><code>type input-*.json | jq &quot;[.foo[].bar]&quot; &gt; output.json
  </code></pre>
  <p>Uh, oh; it gives me the following:</p>
  <pre><code>[{…}, {…}, {…}], [{…}, {…}]
  </code></pre>
  <p>But why? At the point of <code>.foo[].bar</code>, JQ just sees a stream of objects, right? How does JQ &quot;remember&quot; that some of those objects came from different inputs?</p>
  <p>Note that <a href="https://stackoverflow.com/q/29404575">How to convert a JSON object stream into an array with jq</a> and <a href="https://stackoverflow.com/q/38061346">jq: output array of json objects</a> seem to be similar questions. They say to use JQ's <code>--slurp</code> mode. But won't that prevent streaming, i.e. if the output of all the files is really huge, won't it this have the potential of running out of memory by loading the entire input into memory?</p>
  <p>Besides (and this is the crux of my confusion), if <code>jq &quot;.foo[].bar&quot;</code> gives me exactly the <code>{…}, {…}, {…}], [{…}, {…}</code> array contents I want, how does JQ &quot;remember&quot; that some of those objects came from different inputs? You'll see that JQ isn't wrapping each <code>{…}</code> in an array, but actually wrapping several objects based upon which <code>input-*.json</code> file it came from. Why?</p>