<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>MuleSoft — Bowmansarrow</title>
    <link>https://jeffbowman.writeas.com/tag:MuleSoft</link>
    <description></description>
    <pubDate>Tue, 24 Nov 2026 14:37:36 +0000</pubDate>
    <item>
      <title>Traversing Nested Data Structures with DataWeave</title>
      <link>https://jeffbowman.writeas.com/traversing-nested-data-structures-with-dataweave?pk_campaign=rss-feed</link>
      <description>The problem… A friend of mine sent a request on Slack today, he asked if anyone had tried to solve a particular problem he had found post...</description>
      <content:encoded><![CDATA[<h1 id="the-problem-x2026">The problem…</h1>

<p>A friend of mine sent a request on Slack today, he asked if anyone had tried to solve a particular problem he had found posted on <a href="https://twitter.com/josevalim/status/1379771275627921409" rel="nofollow ugc">twitter</a>. The post references <a href="https://github.com/josevalim/nested-data-structure-traversal" rel="nofollow ugc">this</a> GitHub page where the problem and some of the solutions are described. My friend specifically asked if anyone had solved this with pure DataWeave, a transformation language embedded in the <a href="https://www.mulesoft.com" rel="nofollow ugc">MuleSoft</a> software known as Mule.</p>

<p>I haven&#39;t coded anything in DataWeave for quite a while, so I thought I&#39;d give it a go. I had to read up on some of the functions, and then use <a href="https://dwlang.fun" rel="nofollow ugc">this REPL</a> to do the work. I copied the example input from the GitHub page, and started hacking away.</p>

<h2 id="a-first-pass-x2026">A first pass…</h2>

<p>At first I thought I could use some nifty recursion and a “stream” data structure. Basically, that is a pair, the first part is the current value, the second is a function which returns a pair with the next value and a function. That didn&#39;t work out because, well, recursion. DataWeave is not tail-call optimized and will overflow the stack at 256 frames.</p>

<p>Bummer.</p>

<h2 id="a-second-pass-x2026">A second pass…</h2>

<p>Okay, so looking at it from a different perspective, I just needed to setup the indices correctly and then I could iterate over the payload and come up with a solution.</p>

<pre><code>%dw 2.0
output application/json
var allWithReset = zip(payload map $.reset_lesson_position,  payload.lessons map $$ )
var allStartVals = allWithReset map if ($[0] == true) 0 else $[1] + $[1]
---
payload map (value, index) -&gt; {
    title: value.title,
    reset_lesson_position: value.reset_lesson_position,
    position: index + 1,
    lessons: value.lessons map {
        name: $.name,
        pos: allStartVals[index] + ($$ + 1)
    }
}
</code></pre>

<p>That did the trick… with the 3 records in the example data set. But I decided to add another record. And, well, you can guess what happened next… yep, the indices were off – completely. Obviously, the problem is how I was creating the indices, so let&#39;s look there for a solution:</p>

<pre><code>%dw 2.0
output application/json
var allWithReset = zip(payload map $.reset_lesson_position,  payload.lessons map $$ )
var allStartVals = allWithReset map if ($[0] == true) 0 else $[1] + $[1]
var allStartNormalized = allStartVals map if (allStartVals[$$ - 1] == 0) 2 else allStartVals[$$]
---
payload map (value, index) -&gt; {
    title: value.title,
    reset_lesson_position: value.reset_lesson_position,
    position: index + 1,
    lessons: value.lessons map {
        name: $.name,
        pos: allStartNormalized[index] + ($$ + 1)
    }
}
</code></pre>

<p>Yep, that fixed it! Well, at least until I added another record and changed one of the reset values… And then the indices are broken again. So, the solution is just to add more complexity, right!??!</p>

<pre><code class="language-json">%dw 2.0
output application/json
var allWithReset = zip(payload map $.reset_lesson_position,  payload.lessons map $$ )
var allStartVals = allWithReset map if ($[0] == true) 0 else $[1] + $[1]
var allStartNormalized = allStartVals map 
    if (allStartVals[$$ - 1] == 0 and allStartVals[$$] == 0) 
        0 
    else if (allStartVals[$$ - 1] == 0 and allStartVals[$$] != 0) 
        2 
    else allStartVals[$$]
---
payload map (value, index) -&gt; {
    title: value.title,
    reset_lesson_position: value.reset_lesson_position,
    position: index + 1,
    lessons: value.lessons map {
        name: $.name,
        pos: allStartNormalized[index] + ($$ + 1)
    }
}
</code></pre>

<p>Nope, that fails too. At this point I start getting a little banter in the slack channel. We discuss the merits of different approaches to a solution, then one of my friends posts a working solution that is elegant and, after thinking about it, somewhat obvious. As a matter of fact, after saying recursion several times, I realized I needed to be reaching for the <code>reduce</code> function, which is how DataWeave manages recursion.</p>

<h2 id="the-solution-x2026">The solution…</h2>

<p>Nope, not going to post it here, you&#39;ll have to go read this <a href="https://javastreets.com/blog/dataweave/mule-dataweave-nested-data-traversal-with-reduce.html" rel="nofollow ugc">blog post</a> to get the answer. This guy writes some nice blog posts as well, so feel free to peruse and learn a few things. Good stuff!!</p>

<p>Tags: <a href="https://jeffbowman.writeas.com/tag:MuleSoft" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">MuleSoft</span></a> <a href="https://jeffbowman.writeas.com/tag:Dataweave" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">Dataweave</span></a></p>
]]></content:encoded>
      <guid>https://jeffbowman.writeas.com/traversing-nested-data-structures-with-dataweave</guid>
      <pubDate>Sat, 17 Apr 2021 00:41:38 +0000</pubDate>
    </item>
  </channel>
</rss>