title: Generalized way to extract JSON from a relational database?
body: <p>Ok, maybe this is too broad for StackOverflow, but is there a good, generalized way to assemble data in relational tables into hierarchical JSON?</p>
  
  <p>For example, let's say we have a "customers" table and an "orders" table. I want the output to look like this:</p>
  
  <pre><code>{
      "customers": [
          {
              "customerId": 123,
              "name": "Bob",
              "orders": [
                  {
                      "orderId": 456,
                      "product": "chair",
                      "price": 100
                  },
                  {
                      "orderId": 789,
                      "product": "desk",
                      "price": 200
                  }
              ]
          },
          {
              "customerId": 999,
              "name": "Fred",
              "orders": []
          }
      ]
  }    
  </code></pre>
  
  <p>I'd rather not have to write a lot of procedural code to loop through the main table and fetch orders a few at a time and attach them. It'll be painfully slow.</p>
  
  <p>The database I'm using is MS SQL Server, but I'll need to do the same thing with MySQL soon. I'm using Java and JDBC for access. If either of these databases had some magic way of assembling these records server-side it would be ideal.</p>
  
  <p>How do people migrate from relational databases to JSON databases like MongoDB?</p>