title: Bash - Convert MONGODB BSON to JSON
body: <p>I have a shell command that calls a MongoDB command and produces a BSON output. I then want to parse that output with jq so I need to convert the BSON to JSON using jq's tojson.</p>
  
  <pre><code>echo "db._adminCommand({replSetGetStatus : 1})" | /path/to/mongo
  </code></pre>
  
  <p>How do I implement tojson is it as simple as doing this say:</p>
  
  <pre><code>echo "db._adminCommand({replSetGetStatus : 1})" | /path/to/mongo | jq '[.[]|tojson]'
  </code></pre>
  
  <p>I haven't put jq on my server yet as I have to prove it works before I can get it released.</p>
  
  <p>When I try .[]|tojson in the online demo it doesn't work but I wasn't sure if this was a demo limitation?</p>
  
  <p><em>EDIT</em></p>
  
  <p>So I managed to get "jq" on a test server running mongodb and tried this:</p>
  
  <pre><code>echo "db._adminCommand({replSetGetStatus : 1})" | /path/to/mongo | ./jq '.members[] | {Server: .name, State: .stateStr}'
  </code></pre>
  
  <p>The error I received was this:</p>
  
  <pre><code>parse error: Invalid numeric literal at line 1, column 8
  </code></pre>
  
  <p>So it would appear that "jq" can not parse the data from replSetGetStatus.</p>
  
  <p>In the mean time as I sit back scratching my head, waiting for the Change Management Death Squads to turn up, if anyone has some ideas they would be greatly appreciated.</p>