title: Grouping JSON data on multiple keys and retaining the keys
body: <p>I have a flat JSON file that looks like this:</p>
  
  <pre><code>{"dataSet": {"dataTable": [{
      "id": "List1",
      "row": [
          {
              "pagenumber": "7",
              "pageversion": "HE; K12",
              "category": "3D Print - K12;HE",
              "pagetype": "Product",
              "PtrChild": "MakerBot - 10771",
              "Allocation": "0.500",
              "catext": "Text goes here"
          },
          {
              "pagenumber": "7",
              "pageversion": "SL",
              "category": "3D Print - SL",
              "pagetype": "Product",
              "PtrChild": "AUTODESK - 10032",
              "Allocation": "0.500",
              "catext": "Text goes here"
          },
          {
              "pagenumber": "10",
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Brand",
              "PtrChild": null,
              "Allocation": "1.000",
              "catext": "Text goes here"
          },
          {
              "pagenumber": "11",
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Product",
              "PtrChild": "EPSON INK JET** - 10082",
              "Allocation": "0.200",
              "catext": "Text goes here"
          },
          {
              "pagenumber": "11",
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Product",
              "PtrChild": "EPSON INK JET** - 10082",
              "Allocation": "0.200",
              "catext": "Text goes here"
          },
          {
              "pagenumber": "11",
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Product",
              "PtrChild": "EPSON INK JET** - 10082",
              "Allocation": "0.500",
              "catext": "Text goes here"
          },
          {
              "pagenumber": "11",
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Product",
              "PtrChild": "LEXMARK** - 10151",
              "Allocation": "0.200",
              "catext": "Text goes here"
          }
      ]
  }]}}
  </code></pre>
  
  <p>I need to group the data by pagenumber, then by pageversion, then by PtrChild, then by Allocation, so that it looks like this when finished:</p>
  
  <pre><code>{"pages": [
      {
          "pagenumber": "7",
          "versions": [
              {
                  "pageversion": "HE; K12",
                  "category": "3D Print - K12;HE",
                  "pagetype": "Product",
                  "partners": [{
                      "PtrChild": "MakerBot - 10771",
                      "allocations": [{
                          "Allocation": "0.500",
                          "ads": [{"catext": "Text goes here"}]
                      }]
                  }]
              },
              {
                  "pageversion": "SL",
                  "category": "3D Print - SL",
                  "pagetype": "Product",
                  "partners": [{
                      "PtrChild": "AUTODESK - 10032",
                      "allocations": [{
                          "Allocation": "0.500",
                          "ads": [{"catext": "Text goes here"}]
                      }]
                  }]
              }
          ]
      },
      {
          "pagenumber": "10",
          "versions": [{
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Brand",
              "partners": [{
                  "PtrChild": null,
                  "allocations": [{
                      "Allocation": "1.500",
                      "ads": [{"catext": "Text goes here"}]
                  }]
              }]
          }]
      },
      {
          "pagenumber": "11",
          "versions": [{
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Product",
              "partners": [
                  {
                      "PtrChild": "EPSON INK JET** - 10082",
                      "allocations": [
                          {
                              "Allocation": "0.250",
                              "ads": [
                                  {"catext": "Text goes here"},
                                  {"catext": "Text goes here"}
                              ]
                          },
                          {
                              "Allocation": "0.500",
                              "ads": [{"catext": "Text goes here"}]
                          }
                      ]
                  },
                  {
                      "PtrChild": "LEXMARK** - 10151",
                      "allocations": [{
                          "Allocation": "0.200",
                          "ads": [{"catext": "Text goes here"}]
                      }]
                  }
              ]
          }]
      }
  ]}
  </code></pre>
  
  <p>Running this:</p>
  
  <pre><code>var myGroupedData = nest(myCatalog, ["pagenumber", "pageversion", "PtrChild", "Allocation"]);
  // Reorganize JSON data
  function nest(collection, keys) {
      if (!keys.length) {
          return collection;
      }
      else {
          return _(collection).groupBy(keys[0]).mapValues(function(values) { 
              return nest(values, keys.slice(1));
          }).value();
      }
  }
  </code></pre>
  
  <p>...gets the grouping right, but eliminates the Keys for each group:</p>
  
  <pre><code>{
      "7": {
          "HE; K12": {"MakerBot - 10771": {"0.500": [{
              "pagenumber": "7",
              "pageversion": "HE; K12",
              "category": "3D Print - K12;HE",
              "pagetype": "Product",
              "PtrChild": "MakerBot - 10771",
              "Allocation": "0.500",
              "catext": "Text goes here"
          }]}},
          "SL": {"AUTODESK - 10032": {"0.500": [{
              "pagenumber": "7",
              "pageversion": "SL",
              "category": "3D Print - SL",
              "pagetype": "Product",
              "PtrChild": "AUTODESK - 10032",
              "Allocation": "0.500",
              "catext": "Text goes here"
          }]}}
      },
      "10": {"Apply to All": {"null": {"1.000": [{
          "pagenumber": "10",
          "pageversion": "Apply to All",
          "category": "Secure Printers",
          "pagetype": "Brand",
          "PtrChild": null,
          "Allocation": "1.000",
          "catext": "Text goes here"
      }]}}},
      "11": {"Apply to All": {
          "EPSON INK JET** - 10082": {
              "0.200": [
                  {
                      "pagenumber": "11",
                      "pageversion": "Apply to All",
                      "category": "Secure Printers",
                      "pagetype": "Product",
                      "PtrChild": "EPSON INK JET** - 10082",
                      "Allocation": "0.200",
                      "catext": "Text goes here"
                  },
                  {
                      "pagenumber": "11",
                      "pageversion": "Apply to All",
                      "category": "Secure Printers",
                      "pagetype": "Product",
                      "PtrChild": "EPSON INK JET** - 10082",
                      "Allocation": "0.200",
                      "catext": "Text goes here"
                  }
              ],
              "0.500": [{
                  "pagenumber": "11",
                  "pageversion": "Apply to All",
                  "category": "Secure Printers",
                  "pagetype": "Product",
                  "PtrChild": "EPSON INK JET** - 10082",
                  "Allocation": "0.500",
                  "catext": "Text goes here"
              }]
          },
          "LEXMARK** - 10151": {"0.200": [{
              "pagenumber": "11",
              "pageversion": "Apply to All",
              "category": "Secure Printers",
              "pagetype": "Product",
              "PtrChild": "LEXMARK** - 10151",
              "Allocation": "0.200",
              "catext": "Text goes here"
          }]}
      }}
  }
  
  </code></pre>
  
  <p>How can I retain the keys and group on multiple keys?</p>
  
  <p>I'm forced to use ExtendScript, so can't use Lodash beyond version 3.10.1.</p>