title: Splitting Json Array and groupping in to multiple messages camel
body: <p>I am using Jboss fuse 6.3 and apache camel 2.17.I wanted to split a huge json message in to multiple messages in camel spring dsl.</p>
  
  <p>Sample message structure is below in this message i have 9 arrays of countries i need to split in to array of two as 5 messages the last message contains only one array.The order of messages doesn't matter.</p>
  
  <p>Input message:</p>
  
  <pre><code>[{
          "Cities": [{
  
              "name": "Paris",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Marseille",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Lyon",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Toulouse",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Nice",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "FR"
      },
      {
          "Cities": [{
  
              "name": "Berlin",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Hamburg",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Munich",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Cologne",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Frankfurt",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "DE"
      }, {
          "Cities": [{
  
              "name": "mumbai",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Bangalore",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Delhi",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Chattisgarh",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Hyderabad",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "IN"
      }, {
          "Cities": [{
  
              "name": "Aberdeen",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Armagh",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Bangor",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Bath",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Belfast",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "UK"
      }, {
          "Cities": [{
  
              "name": "Budapest",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Debrecen",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Szeged",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Miskolc",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Pecs",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "HUNGARY"
      }, {
          "Cities": [{
  
              "name": "Alingsas",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Arboga",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Arvika",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Askersund",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Avesta",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "SWEDEN"
      }, {
          "Cities": [{
  
              "name": "Amsterdam",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Den Helder",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Edam",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Haarlem",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Purmerend",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "NETHERLANDS"
      }, {
          "Cities": [{
  
              "name": "Abrantes",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Agualva-Cacem",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Agueda",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Albufeira",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Alcacer do Sal",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "PORTUGAL"
      }, {
          "Cities": [{
  
              "name": "Barcelona",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "madrid",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Saragossa",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Salamanca",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Granada",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "SPAIN"
      }
  ]
  </code></pre>
  
  <p>Expected output:</p>
  
  <p>First split:</p>
  
  <pre><code>[{
          "Cities": [{
  
              "name": "Paris",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Marseille",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Lyon",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Toulouse",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Nice",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "FR"
      },
      {
          "Cities": [{
  
              "name": "Berlin",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Hamburg",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Munich",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Cologne",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Frankfurt",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "DE"
      }
  ]
  </code></pre>
  
  <p>Second split:</p>
  
  <pre><code>[{
          "Cities": [{
  
              "name": "mumbai",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Bangalore",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Delhi",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Chattisgarh",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Hyderabad",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "IN"
      },{
          "Cities": [{
  
              "name": "Aberdeen",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "Armagh",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Bangor",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Bath",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Belfast",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "UK"
      }
  ]
  </code></pre>
  
  <p>.
  .
  .</p>
  
  <p>Fifth split:</p>
  
  <pre><code>[{
          "Cities": [{
  
              "name": "Barcelona",
              "lat": "45.75",
              "lng": "4.583333"
          }, {
  
              "name": "madrid",
              "lat": "45.97",
              "lng": "4.54"
          }, {
  
              "name": "Saragossa",
              "lat": "24.75",
              "lng": "4.75"
          }, {
  
              "name": "Salamanca",
              "lat": "37.75",
              "lng": "4.755"
          }, {
  
              "name": "Granada",
              "lat": "89.75",
              "lng": "4.77"
          }],
          "country": "SPAIN"
      }
  ]
  </code></pre>
  
  <p>I tried below referring  <a href="https://stackoverflow.com/questions/47796217/apache-camel-with-json-array-split">Apache Camel with Json Array split</a> from the post of Ricardo.But iam not sure how to configure aggregate(AggregationStrategies.groupedExchange()) in spring dsl.</p>
  
  <p>Camel route:</p>
  
  <pre><code>&lt;split streaming="true"&gt;
                      &lt;jsonpath&gt;$&lt;/jsonpath&gt;
                      &lt;aggregate completionTimeout="1000" completionSize="2" groupExchanges="true"&gt;
  
                      &lt;/aggregate&gt;
  
                      &lt;log message="${body}"&gt;&lt;/log&gt;
                  &lt;/split&gt;
  </code></pre>