Jeroen de Beer
Jeroen de Beer

Reputation: 340

How do I Loop over json and post every index in camelcontext

Let's say I have the following CamelContext:

<camelContext id="camelId"  xmlns="http://camel.apache.org/schema/spring">
    <route id="upsertItem">
        <from uri="cxf:bean:someEndpoint" />
        <process ref="someTransformer" />
        <log message="$someJson"/>
    </route>
</camelContext>

$someJson contains the following json:

{
   "0":{
      "title":"hello world",
      "description":"a greeting to the world"
   },
   "1":{
      "title":"goodbye world",
      "description":"a goodbye to the world"
   }
   //might have more indexes
}

I want to edit my CamelContext so it can post every index to an endpoint. The problem is that I can't find out how to iterate through the JSON.

So I want to have something that looks like this:

<loop src="${someJson[index]}">
    <to uri="http://bookstore/api/books"/>
</loop>

Upvotes: 0

Views: 471

Answers (1)

Greenev
Greenev

Reputation: 909

It could be done by splitting the original message

<split>
    <jsonpath>$[*]</jsonpath>
    <log message="${body}"/>
</split>

Upvotes: 2

Related Questions