1

Want to extract each object from my json payload which looks like this: Need to pass each object from this map into splitter or collection splitter. I cannot use foreach scope here.

[
    {
        "Name": null,
        "Key": "4",
        "Header": {
        "Id": "14"
        }
    },
    {
        "Name": null,
        "Key": "5",
        "Header": {
        "Id": "15"
        }
    }
]
Simon Karlsson
  • 4,090
  • 22
  • 39
user4338724
  • 91
  • 2
  • 7

3 Answers3

0

To get the entire object from the array we can use simple #[payload] and this will take care just like for each scope.

user4338724
  • 91
  • 2
  • 7
0

If you want to parse and access the JSON elements then **<json:json-to-object-transformer/>** is the way to do the trick. java.util.HashMap or java.util.List or java.util.Map depends on the type of your JSON data. You can also use java.lang.Object

vijay dhanakodi
  • 175
  • 1
  • 14
  • You can also use #[json:data/field] – vijay dhanakodi Mar 10 '17 at 05:04
  • I would recommending not using the `#[json:data/field]` expression. The JsonPath expression evaluator was deprecated. More info. on this can be found here: https://docs.mulesoft.com/mule-user-guide/v/3.8/json-module-reference. – Paul Calabro Mar 11 '17 at 20:51
  • I am still getting exception. even if i use java.util.List or java.lang.Object. org.mule.api.transformer.TransformerMessagingException: The object transformed is of type: "SimpleDataType{type=java.lang.String, mimeType='*/*', encoding='null'}", but the expected return type is "SimpleDataType{type=java.util.List, mimeType='application/json', encoding='null'}". (org.mule.api.transformer.TransformerMessagingException) – user4338724 Mar 13 '17 at 03:47
  • After collection splitter, had to add object to json transformer to get rid of the above exception – user4338724 Mar 13 '17 at 04:35
0

There are multiple ways to do this : 1) As mentioned by Vijay, use json-to-object transformer to map input json to any Collection type e.g.

 <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/>
  <collection-splitter doc:name="Collection Splitter"/>

2) Use dataweave to get a Collection from input json data :

<dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload 
]]></dw:set-payload>
        </dw:transform-message>
<collection-splitter doc:name="Collection Splitter"/>

  • Getting the below Exception when used dataweave script as suggested Root Exception stack trace: java.lang.IllegalArgumentException: Object "com.mulesoft.weave.reader.ByteArraySeekableStream" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" at org.mule.util.collection.EventToMessageSequenceSplittingStrategy.split(EventToMessageSequenceSplittingStrategy.java:65) – user4338724 Mar 13 '17 at 01:09
  • What is your input type to dataweave ? is it json or something else? – user02041988 Mar 13 '17 at 22:55