title: Combine records into a single JSON object per Category using Dataweave
body: <p>I am using Anypoint Studio 6.1 and Mule 3.8.1 and in Dataweave I have a CSV file as input and JSON list output.  </p>
  
  <p>Multiple records are coming in on the CSV file with the same Product Category Id but will have a different product details associated with it.  I want my output to be aggregated for each of these records so I have one object for each Product Category Id which contains a list of the product details for each product under that Product Category id.  How can I do this?</p>
  
  <p><strong>Current output:</strong></p>
  
  <pre><code>[
    {
      "Products": {
        "ProductDetails": [
          {
            "ProductDetail": {
              "ProductSubDetails": {
                "ProductAmount": 7.50,
                "ProductReplenFrequency": "monthly",
                "ProductsNotes": "Product Notes 1",
                "pick": false
              },
              "ProductType": "PS4 Game"
            },
            "ProductSubType": "Game"
          }
        ]
      },
      "ProductsCategoryId": "ProductS001",
      "ProductSubType": "Computers and Games"
    },
    {
      "Products": {
        "ProductDetails": [
          {
            "ProductDetail": {
              "ProductSubDetails": {
                "ProductAmount": 7.50,
                "ProductReplenFrequency": "fortnightly",
                "ProductsNotes": "Products Notes 2",
                "pick": false
              },
              "ProductType": "X Box One Game"
            },
            "ProductSubType": "Game"
          }
        ]
      },
      "ProductsCategoryId": "ProductS001",
      "ProductSubType": "Computers and Games"
    }
  ]
  </code></pre>
  
  <p><strong>Expected output:</strong></p>
  
  <pre><code>[
    {
      "Products": {
        "ProductDetails": [
          {
            "ProductDetail": {
              "ProductSubDetails": {
                "ProductAmount": 7.50,
                "ProductReplenFrequency": "monthly",
                "ProductsNotes": "Product Notes 1",
                "pick": false
              },
              "ProductType": "PS4 Game"
            },
            "ProductSubType": "Game"
          },
          {
            "ProductDetail": {
              "ProductSubDetails": {
                "ProductAmount": 7.50,
                "ProductReplenFrequency": "fortnightly",
                "ProductsNotes": "Products Notes 2",
                "pick": false
              },
              "ProductType": "X Box One Game"
            },
            "ProductSubType": "Game"
          }       
        ]
      },
      "ProductsIdentifier": "ProductS001",
      "ProductSubType": "Computers and Games"
    }
  ]
  </code></pre>
  
  <p><strong>Dataweave code:</strong></p>
  
  <pre><code>%dw 1.0
  %input payload application/csv
  %output application/json skipNullOn = "everywhere" , encoding = "UTF-8"
  %var dataLookup = {(payload."ProductsCategoryId" map {($.Id): $.Value})}
  
  ---
  (payload filter $$ &gt; 2) map ((payload01 , indexOfPayload01) -&gt; {
      Products: {
          ProductsDetails: [{
              ProductsDetail: {
                  ProductsubDetails: {
                      ProductsAmount: payload01."Products Amount" as :number,
                      ProductsFrequency: payload01."Products Frequency"
                  },
                  ProductsType: payload01."Products Type"
              }
          }]
      },
      ProductsCategoryId: payload01."ProductsCategoryId"
  })
  </code></pre>
  
  <p><strong>Sample Data:</strong></p>
  
  <pre><code>ProductsCategoryId,Product Type,Product  Frequency,Product Amount
  ProductS001,PS4 Game,Monthly,7.5
  ProductS001,X Box One Game,Fortnightly,7.5
  ProductS002,Lego,Daily,7
  </code></pre>
  
  <p>Thanks</p>