title: Flatten a json datastructure in javascript
body: <p>I have a json response coming from a service that's a bit of a pain to work with. How can I go about flattening this structure a bit so that  <code>"not_restricted":{"doc_count": 976465}</code> is changed to <code>"not_restricted":976465</code>? I'd perfer a function that can detect this kind of a structure in all types of json docs and modify the result.</p>
  
  <p>This is a the general structure of the json:</p>
  
  <pre><code>{
      "took": 159,
      "timed_out": false,
      "_shards": {
          "total": 6,
          "successful": 6,
          "failed": 0
      },
      "hits": {
          "total": 4909332,
          "max_score": 1,
          "hits": [
              {
                  ...
              },
              {
                  ...
              },
              {
                  ...
              }
          ]
      },
      "aggregations": {
          "index_types": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                  {
                      "key": "client",
                      "doc_count": 1958205,
                      "not_restricted": {
                          "doc_count": 976465
                      },
                      "restricted": {
                          "doc_count": 981740
                      }
                  },
                  {
                      "key": "ultimateparent",
                      "doc_count": 1616164,
                      "not_restricted": {
                          "doc_count": 741059
                      },
                      "restricted": {
                          "doc_count": 875105
                      }
                  },
                  {
                      "key": "facility",
                      "doc_count": 1334963,
                      "not_restricted": {
                          "doc_count": 914090
                      },
                      "restricted": {
                          "doc_count": 420872
                      }
                  }
              ]
          }
      }
  }
  </code></pre>