1
votes

I have json response that I need to break into multiple parts before sending it to target system.

I was trying with for-each but couldn't reach my desired output.

input json look like this

{
    parent :
    {
        child:
            [{
                a:[{},{},{},{}]
            }]  
    }
}

and out should look like this part1

{
    parent :
    {
        child:
            [{
                a:[{},{}]
            }]  
    }
}

part2

{
    parent :
    {
        child:
            [{
                a:[{},{}]
            }]  
    }
}

Can someone please help me out here?

1
Updated answer, since the question was edited to change the input/ouptutRyan Carter

1 Answers

3
votes

It is hard to tell based on your sample json as its not formatted json.

NOTE The expected input/output changed from my original answer so this is an update

However, this works and can tweaked based on your actual input.

First, use foreach to iterate the collection part of your payload(based on your example it is #[flatten(payload.parent.child.a)] (but this might change as its not clear if the 'child' array would also more than one element.)

You also don't mention how you want to split it. Based on your example, it is for every 2 records. So use the batchSize attribute and set to 2(change this to your actual requirement).

Then you need to add the the wrapping json elements back, as you will lose that in the foreach:

  <foreach doc:name="For Each"  collection="#[flatten(payload.parent.child.a)]" batchSize="2">
            <ee:transform doc:name="Transform Message" doc:id="7b1cccb7-fcbb-41b3-a08f-bac5600df2f2" >
                <ee:message >
                    <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
    parent :
    {
        child:[
           a: payload
        ]
    }
}]]></ee:set-payload>
                </ee:message>
            </ee:transform>
            <logger level="ERROR" message="Split items here: #[payload]" />
        </foreach>

The output matches your new expected output:

  Split items here: {
      "parent": {
    "child": [
      {
        "a": [
          {

          },
          {

          }
        ]
      }
    ]
  }
}

Split items here: {
  "parent": {
"child": [
  {
    "a": [
      {

      },
      {

      }
    ]
  }
]

} }