Create a message from a collection of collections in MULE ESB -


i have json like:

{ "editors" : [{         "editor" : "mcgram hill",         "books" : [{                 "name" : "differential calculus",                 "year" : "1995",                 "times_read" : "135"             }, {                 "name" : "2012 end of world",                 "year" : "2012,",                 "times_read" : "56"             }         ]     }, {         "editor" : "demidovich",         "books" : [{                 "name" : "some tittle",                 "year" : "1975,",                 "times_read" : "154"             }, {                 "name" : "the little prince",                 "year" : "1987,",                 "times_read" : "57"             }         ]     }, {         "editor" : "adrian lopez asc.",         "books" : [{                 "name" : "something",                 "year" : "2008,",                 "times_read" : "10"             }         ]     } ] 

}

i need transform output like:

payload[0]= editor mcgram hill has: book differential calculus published in 1995 has been readed 135 times. book 2012 end of world published in 2012 has been readed 56 times.  payload[1]= editor demidovich has: book tittle published in 1975 has been readed 154 times. book little prince published in 1987 has been readed 57 times.  payload[2]= editor adrian lopez asc. has: book published in 2008 has been readed 10 times. 

so far transform json java object, use foreach scope collection #[payload.editors] inside foreach create variable intro = "the editor #[payload.editor] has:" , after i'm lost. have tried set payload #[payload.books] , use foreach shows last book, , have try collection aggregator don't know write in "message info mapping".

how can accomplish required output?

any appreciated, in advance.

try following dataweave

%dw 1.0 %output application/json --- {(payload.editors map ((data, index) -> {     payload : "the editor " ++ data.editor ++ " has:\r\n" ++      {(data.books map {         pqr : "the book " ++ $.name ++ " published in " ++ $.year ++ " has been readed " ++ $.'times_read' ++ " times."     })} pluck $ joinby "\r\n" }))} pluck $ 

json output reference. can transform java desired output.

[   "the editor mcgram hill has:\r\nthe book differential calculus published in 1995 has been readed 135 times.\r\nthe book 2012 end of world published in 2012, has been readed 56 times.",   "the editor demidovich has:\r\nthe book tittle published in 1975, has been readed 154 times.\r\nthe book little prince published in 1987, has been readed 57 times.",   "the editor adrian lopez asc. has:\r\nthe book published in 2008, has been readed 10 times." ] 

hope helps.


Comments