title: Best way to produce a flattened JSON (denormalize) out of hierarchical JSON in Ruby
body: <p>I'm trying to achieve the following. My input JSON looks like this,</p>
  
  <pre><code>{
     "data":{
        "shipping_address":[
           {
              "cust_id":"CUST-123",
              "street":"123 Main St",
              "city":"Atlanta",
              "state":"GA",
              "zip":"12345"
           },
           {
              "cust_id":"CUST-456",
              "street":"456 Front St",
              "city":"Philadelphia",
              "state":"PA",
              "zip":"23456"
           }
        ],
        "orders":[
           {
              "cust_id":"CUST-456",
              "items":[
                 {
                    "quantity":"2",
                    "item_code":"ABC-111-222",
                    "cust_id":"CUST-456"
                 },
                 {
                    "quantity":"1",
                    "item_code":"DEF-999-01-001",
                    "cust_id":"CUST-456"
                 }
              ]
           },
           {
              "cust_id":"CUST-123",
              "items":[
                 {
                    "quantity":"10",
                    "item_code":"998-111-222",
                    "cust_id":"CUST-123"
                 }
              ]
           }
        ],
        "payment":[
           {
              "cust_id":"CUST-123",
              "type":"VISA",
              "card_no":"1234-1111-2222-3333",
              "expiry":"06/2016",
              "billing_add_same_as_shipping":"Y",
              "first_name":"John",
              "last_name":"Smith"
           },
           {
              "cust_id":"CUST-456",
              "type":"VISA",
              "card_no":"5678-4444-8877-5544",
              "expiry":"08/2016",
              "billing_add_same_as_shipping":"N",
              "first_name":"Steve",
              "last_name":"Jones"
           }
        ],
        "billing_address":[
           {
              "cust_id":"CUST-456",
              "street":"7788 Back St",
              "city":"Gainesville",
              "state":"FL",
              "zip":"33444"
           }
        ]
     }
  }
  </code></pre>
  
  <p>I would like to flatten out this json into two separate jsons</p>
  
  <pre><code>{
     "data":{
        "shipping_address":{
           "cust_id":"CUST-456",
           "street":"456 Front St",
           "city":"Philadelphia",
           "state":"PA",
           "zip":"23456"
        },
        "orders":{
           "cust_id":"CUST-456",
           "items":[
              {
                 "quantity":"2",
                 "item_code":"ABC-111-222",
                 "cust_id":"CUST-456"
              },
              {
                 "quantity":"1",
                 "item_code":"DEF-999-01-001",
                 "cust_id":"CUST-456"
              }
           ]
        },
        "payment":{
           "cust_id":"CUST-456",
           "type":"VISA",
           "card_no":"5678-4444-8877-5544",
           "expiry":"08/2016",
           "billing_add_same_as_shipping":"N",
           "first_name":"Steve",
           "last_name":"Jones"
        },
        "billing_address":{
           "cust_id":"CUST-456",
           "street":"7788 Back St",
           "city":"Gainesville",
           "state":"FL",
           "zip":"33444"
        }
     }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{
     "data":{
        "shipping_address":{
           "cust_id":"CUST-123",
           "street":"123 Main St",
           "city":"Atlanta",
           "state":"GA",
           "zip":"12345"
        },
        "orders":{
           "cust_id":"CUST-123",
           "items":[
              {
                 "quantity":"10",
                 "item_code":"998-111-222",
                 "cust_id":"CUST-123"
              }
           ]
        },
        "payment":{
           "cust_id":"CUST-123",
           "type":"VISA",
           "card_no":"1234-1111-2222-3333",
           "expiry":"06/2016",
           "billing_add_same_as_shipping":"Y",
           "first_name":"John",
           "last_name":"Smith"
        }
     }
  }
  </code></pre>
  
  <p>Is there an easy way in Ruby to perform this without any looping/parsing each fragment of the input json (i.e. by doing any JSON Mapping)?</p>