title: How to use jq with popen to validate json data
body: <p>I am trying to use popen and jq on a Linux machine to validate json data in some unit tests but, for some reason, it does not work, i.e., jq's exit code is not zero. Here is a MWE:</p>
  <pre><code>#include &quot;stdio.h&quot;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  int main()
  {
          std::string command = &quot;echo {} | jq&quot;;
          FILE* pipe = popen(command.c_str(), &quot;r&quot;);
          if (!pipe) return -1;
          char buffer[1024];
          std::stringstream ss;
          while(!feof(pipe)) if(fgets(buffer, 1024, pipe) != NULL) ss &lt;&lt; buffer;
          int exitCode = WEXITSTATUS(pclose(pipe));
          printf(&quot;Exit code: %d\nOutput: %s\n&quot;, exitCode, ss.str().c_str());
  }
  </code></pre>
  <p>However, if I replace 'jq' with (for example) 'head -1', the code works as expected. Am I doing something wrong or is there a bug in popen or jq?</p>