title: MongoDB - How to consume printjson output in Java
body: <p>I need to communicate with MongoDB shell via Java<br/>
  To explain in short, I am using something like Runtime.exec (commons exec specifically) to call <a href="http://www.mongodb.org/display/DOCS/Scripting+the+shell" rel="nofollow">MongoDB shell in --eval mode</a></p>
  
  <p>Using this approach, I am able to successfully invoke MongoDB commands, retrieve the output into a stream (and print to stdout for example)</p>
  
  <p>All is well till this point.</p>
  
  <p><strong>I need to now "parse" this output into something like an array of <a href="http://json.org/javadoc/org/json/JSONObject.html" rel="nofollow">JSONObject</a> <br/></strong>
  (I can make use of any JSON library like Jackson, etc. that may be appropriate for this)
  so that I can "massage" the output based on some requirements. (Change the format, remove some key value pairs, etc.)</p>
  
  <p>When I look at the output generated by printjson in case of arrays at least, it does not seem to be a valid json array but rather valid json objects separated by new line without the comma in between or enclosing [] that would make it a valid array.</p>
  
  <p>In this case, any suggestions on how to parse this output into some kind of holder objects that I can use to read/modify key value pairs?</p>
  
  <p>I don't have the json commands at compile time but do know that these commands will always end in a printjson</p>
  
  <p><strong>EDIT -</strong></p>
  
  <p>So to simplify, for example, I am looking for a way to parse the following output into Java Objects -<br/>
  (The individual key/value pairs are not known at compile time)</p>
  
  <pre><code>
  {
      "_id" : ObjectId("4db0e0289523f82ff6cd90d4"),
      "calendar_date" : "2009-11-10",
      "product_id" : 8,
      "channel" : "website",
      "country" : "USA",
      "total_unit_count" : 740,
      "total_amount" : 11367.29
  }
  {
      "_id" : ObjectId("4db0e0289523f82ff6cd90d5"),
      "calendar_date" : "2009-11-10",
      "product_id" : 8,
      "channel" : "website",
      "country" : "Australia",
      "total_unit_count" : 740,
      "total_amount" : 13893.09
  }
  {
      "_id" : ObjectId("4db0e0289523f82ff6cd90d6"),
      "calendar_date" : "2009-11-10",
      "product_id" : 8,
      "channel" : "retail",
      "country" : "USA",
      "total_unit_count" : 13,
      "total_amount" : 8296.89
  }
  </code></pre>