Dataweave add to array. Use the Collection field for this purpose.
Dataweave add to array. If you already have a I know in DataWeave the vars are immutable, so I was attempting to copy the existing map to a new Map and adding the new key/value pair. 0 (Mule 4). However, those two will return an Object, as opposed to pluck that returns an Array. Dec 10, 2022 · I have a basic dataweave mapping function. Jun 10, 2022 · Sometimes we get a data structure with keys and values and we want to create an array with data from the data structure. In the below example, we will be iterating through an array of JSON objects which includes a person's full name. Other versions act on strings, objects, and the various date and time formats that DataWeave supports. Please suggest how to do this. value map ((field) -> (field. In the DataWeave script below, a function is called that determines if a Simpson is of majority and can therefore vote. May 18, 2017 · Create json array using dataweave Asked 7 years, 11 months ago Modified 7 years ago Viewed 5k times I have an array of strings in the input JSON Items ["item1", "item2", "item3"] need to map to a String like "item1, item2, item3" Is there a way to do it? enricorafolsdelacruz (Customer) 7 years ago try this ` { id : vars. Oct 8, 2024 · Discover practical examples of MuleSoft's DataWeave for transforming data between formats like JSON, XML, and CSV in this comprehensive guide. You can create a variable in dataweave using # [ []] as empty list and then add payload into another dw which is inside the loop as vars. { (…)}. To do this, developer must wrap the element inside a parenthesis ` ( )` and add a conditional expression `when something`. Hope this helps. If there are 3 params, then three AND conditions, etc. It will wrap all elements to one object Dec 11, 2023 · I have a requirement where I need to convert a String to array or object if present in payload or else return the value as string or boolean. 3. 0 (supported in 4. The first (firstInput) is a DataWeave variable that lists price by book ID, and the second (secondInput) lists authors by book ID. To convert string to array you can use SplitBy function of dataweave 2. Mar 24, 2022 · Can anyone guide me on how to append the values of inside a json array element in dataweave 2. id} ++ payload` in your Transform Message (Dataweave) instead of ` { id : vars. Hi, I have a scenario where I have to add objects to an array if they are not present. finally you have What is DataWeave? DataWeave is an integral part of MuleSoft that helps accelerate its data transformation and integration capabilities. IMPORTANT: The MuleSoft Community Forums have moved to the online Trailblazer Community. Click on the different cookie categories to find out more about each category and to change the default settings. If you already have a Hi, My Input json array is --> [100,200,300,400,500] and my expected output is --> ( '100','200','300','400','500' ) How can i achieve ? @mulsoft @ryanandal DataWeave 1 May 30, 2022 · DataWeave has a standard function to join array elements into a string value: joinBy in the dw::Core module. That saves you from iterating `trips` array multiple times. DataWeave is a functional language used in Mule applications to perform data transformations. find find<T> (@StreamCapable () elements: Array<T>, elementToFind: Any): Array<Number> Returns indices of an input that match a specified value. You use the Expression component: In "Init acc" we set flowVars. Add a Transform Message component and open the DataWeave editor. Each book has a unique bookId key. Jul 17, 2018 · Learn how to transform an array to an object using the DataWeave language. 0 var myInput = read ("<root> <order> <items>1</items> <items>3</items> </order> <order> <items>2</items> </order> </root>", "application/xml") output application DataWeave enables you to pass attributes from within an XML tag input source to the target output XML. Input { "Id": { "EmpId" Nov 28, 2021 · In this tutorial will cover the most common DataWeave functions you will need to work with Arrays. Jan 4, 2021 · Array of objects to a single object containing all said objects Dataweave Asked 4 years, 6 months ago Modified 4 years, 6 months ago Viewed 4k times sum sum (@StreamCapable values: Array<Number>): Number Returns the sum of numeric values in an array. I've tried various ways using nested maps and reduce scripts but having some difficulty. We covered how to use drop, take, and slice to extract subsets of an Array, and the subtle differences between using these functions vs. Mar 7, 2023 · Filtering based on a value inside array of object using DataWeave 2. example- abc xyz back space { "hello":" Apr 12, 2022 · 0 Input - ["eq,"comp",. variable1 default []) Above transform message result will store in the variable1. " In the code above we're using {} in a completely different way now. The goal is to do it effectively into a new one. listVariable << payload Jun 6, 2019 · Conclusion This concludes Part 1 of the new functionality added to the DataWeave Array module in version 2. In Mule 3, the solution to this problem is pretty well understood. update (arrayValue: Array, indexToUpdate: PathElement): UpdaterValueProvider<Array> This update function updates the specified index of an array with the given PathElement value. 0 Add Items to Array Asked 5 years, 3 months ago Modified 5 years, 3 months ago Viewed 4k times Jul 31, 2017 · GOAL When we try to concatenate objects and arrays in DataWeave we can get coercion errors if some objects are composed by arrays. It can be used to transform an Array into any other type. The anonymous function (value, index) → {index: value} maps each item in the input to an object. Jul 5, 2021 · I am learning dataweave and I am trying to extract phoneNumber, with "contact" being array of objects. Apr 7, 2024 · 1. Sep 12, 2022 · The keys in the objects of array 1 should be updated with the corresponding values of array 2, only in the objects where the count value = 1, otherwise it should be left unchanged, thus preserving the order of the initial array. On the 2nd Transform Message inside the for loop, this is the dataweave: I want to add this list into the empty array I declared earlier. 0 Mule Runtime, DataWeave now has three new additions to the Arrays module that give us SQL-esque joining functionality. So based on key name you can use ++ to add field to child object. Build a whole new array with your transformed data. You can choose whether functional and advertising cookies apply. Jun 22, 2021 · Without the *, DW is just doing a blind transformation to a collection just because the object has multiple values for that property. If you come from a different development background, you may know this function as for or forEach. content default []) ++ (vars. @Vinay oboyENREj , You can use + or << operator to append an object to an array. DataWeave gives us the pluck function to achieve this. Jun 28, 2021 · The input directive is not required in some scenarios when the DataWeave execution itself is contextualized and the input derived from said context. This version of the function returns indices of an array. %dw 2. Jul 8, 2020 · You can directly keep payload as it is and add the new JSON field as below: payload ++ {c:3} output: { a:1, b:2, c:3 } May 28, 2020 · What is object concatenation and how to use it in Mule 4 - DataWeave (DW) 2. pluck is similar to other Object-like functions, for example, filterObject or mapObject. Jun 10, 2016 · I am receiving a payload from an API call, and I need to transform a single value into an array with a single element. e. The payloadis in this case expected to be a list of customers (as an array). 0 Hi All, Can you please help me to append an object to array object customerOrder. Jan 15, 2021 · Introduction The map operator is a function in Dataweave which iterates over the items in an array and outputs them into a new array. With input (Getting this input from other system as JSON response) add the hard coded value and have to produce the output json How to achieve this using dataweave Feb 2, 2021 · I have an array with a single index that has a JSON object that contains three different string arrays, which I have to map into a single one based on each index. This is my current flow: Before the for each scope, I declared a variable containing an empty array. Basically I want to gather all payload from the for each and put them into the empty array. DataWeave 2. Each element of the array into is transformed into an object (key-value pairs) and then is concatenated in the accumulator so all key-pairs are joined into a single object. We covered how to use drop, take, and slice to extract subsets of an Array, and the subtle differences between using these functions versus the more familiar slice notation, arr [n to m]. Then, in the "Add result to acc" Expression component within the For Each Dec 8, 2020 · This is how we can transform the array data structure in DataWeave 2. You will also learn how to use pluck in combination with groupBy to achieve a common use case. college. 0 Asked 3 years, 5 months ago Modified 3 years, 5 months ago Viewed 200 times I have similar situation where I have to merge multiple json arrays, and some of the array could be null as well, and I don't want to merge null arrays. Labels map ((label) -> { value: [ { (label. acc to an empty array, []. Adding a new field to the existing payload: In the Json Array input Payload we have added status field to each of the objects in the array. Which am trying different ways but unable to do so please help me. Suppose this senario in the dataweave playground: %dw 2. Change or update each item however you like. However, there are two additional syntax options to concatenate objects and one to concatenate strings in DataWeave. If the payload change and you just get one, it will return one value and not a collection and sum () will fail. ""] (can be 1 to n number of values) Based on the number of input values in the array, number of conditions should be built. Learn the basic concepts of the language, common data structures such as arrays, objects & strings via the interactive editor. Apr 8, 2020 · GOAL To define or construct an array range like 1. We use a lambda to define the mapping from our object to elements in the resulting array. If you come from a different development background, this is something similar to a while or do while. Would it be better, if the script gets very complex and damages readability and eas May 12, 2023 · The DataWeave Playground is a web-based environment that allows you to write and test DataWeave transformations in real-time, without the need to set up any local development environment. Also, how to add the key to the empty array if the matching value exists under it. If you already have a This might be a redundant or silly questionm but I can't seem to find a satisfactory answer or even reproduce answers posted here on how null is handled in dataweave arrays. Map Data with DataWeave This DataWeave example uses the DataWeave map function to iterate through an array of books and perform a series of tasks on each. So when transforming from Java or JSON data structure to XML, it is mandatory to wrap the map operation in curly braces and parenthesis i. How to use Map function to iterate over items in an array and outputs the results into a new array Oct 1, 2017 · This version of ++ concatenates the elements of two arrays into a new array. Write cleaner and easier-to-understand DataWeave Feb 11, 2022 · I am beginner for mulesoft I have output payload transform message as below but I have to replace inside promotions array planChangeRequired attribute value as true always, can some one help me how how to use IF ELSE condition in Dataweave 2. 0. Dec 3, 2022 · Use the zip function to combine the arrays together, and then the ++ operator to merge the objects together: fun combineTwo(input1, input2) = (input1 zip input2) // produces an array of two element arrays map ($[0] ++ $[1]) // combines the two elements into a single object Then you can use reduce to do this with an arbitrary number of input arrays like so: [input1, input2, input3, input4 In Mulesoft Dataweave I want to map an object to an array. It can be used to perform the task of map, filter, distinctBy, groupBy, and other functions that take in Arrays. Jun 14, 2019 · This post will examine the remaining additions to the DataWeave Arrays module that weren’t covered in part 1: the join functions. May 25, 2022 · This article goes over a scenario where specific transformations need to occur to each element in an array of values using DataWeave. Form what I can gather based upon your specific input you should have an empty array. This will iterate over each key. Nov 15, 2018 · Which you could describe as "map the payload by adding the key:value pair of {employed: true} to each object in the array," as opposed to "map the payload by keeping name, job, and age key:value pairs the same, and adding an additional key:value pair of {employed: true}. Jul 20, 2021 · In this article, we describe, discuss, and walk through two different Dataweave 2 approaches (pluck and reduce) to convert between Objects and Arrays. customAttributeList I wanted to add other object to customAttributeList array, I wanted rest of fields As-Is. In another article, we talked about how to remove fields from your contents thanks to Dataweave if you know the key. Jul 3, 2024 · how to add double quotation for each items of array in the dataweave Asked 1 year ago Modified 12 months ago Viewed 137 times Example: Declare an Array Variable and Transform its Elements Variables are immutable in DataWeave, however you can iterate over an array variable and apply a transformation to its elements, which can then be assigned to new variables. 0 output application/csv header=false, separator="|" var headers={ h1: "Header", h2: payload. Jan 25, 2021 · Create an exprected output because your sentence: I want to get an output where I only get all objects from Payload where Errors array has any key with not null values otherwise it should be filtered out. orderLines. Introduced in DataWeave version 2. Also you can do : var arr = [] --- arr + obj1 + obj2 in case you need to add more objects later on Aug 7, 2023 · 4. May 9, 2019 · Conclusion This concludes Part 1 of the new functionality added to the DataWeave Array module in version 2. The input payload is { "mainAttribs": [ { " We use three kinds of cookies on our websites: required, functional, and advertising. In this article, we will cover all the functions with examples and their internal functional definition. inputBy, h3: payload. Returns 0 if the array is empty and produces an error when non-numeric values are in the array. Wrap such array to curly braces. ++ [array] is not allowed against the element list. DataWeave supports each of these values natively. InputJSON: { "0": [ { "text": "Line0-1" Oct 17, 2018 · Simplest approach is to use ++ operator in dataweave like %dw 1. orderLine. every (value: Null, condition: (Nothing) -> Any): Boolean Helper function that enables every to work with a null value. n using DataWeave Dec 12, 2019 · Idea here is that you will have outer loop (iterating over parent array) through map and have an inner loop (iterating over videos). For the value part you can map the values as fieldName: value array and deconstruct them to an object by wrapping the array around parentheses %dw 2. In this post, we'll learn how to use the update operator along with the upsert and conditional options. For clarity, if another dev gets your code and see the *, they will know it's expecting a collection. fieldName): field. Oct 12, 2020 · Iterating over an array of objects using DataWeave Asked 4 years, 6 months ago Modified 4 years, 6 months ago Viewed 1k times Jul 26, 2023 · Can you please share what you have tried? any issue or challenges? Does this answer your question? how to groupBy flatboject array in dataweave Jul 14, 2023 · Add another Set Variable component and set the Name to payload and the Value to the payload JSON given above. Jul 13, 2022 · You can just use map to map all the arrays to required format. 4 output application/json var foo = [ "abc", null, "Banana"] --- foo filter ( $ != null) joinBy "-" Eventhough the filter only keeps elements 0 and 2 of the array, if May 31, 2022 · I have requirement where I need to put together a set of string and a object. I want to have a number of objects with a simple array as input: I have created the function generatePoints to achive this fun generatePoints() = [1 to 2 Jun 30, 2021 · Steps In Dataweave 2. 0 %output application/json --- payload ++ {id : "123"} If you have to update child object you can use mapObject. It supports various data types, enabling users to manipulate strings, numbers, objects, and XML seamlessly. 0 Jan 26, 2025 · The map() function in DataWeave transforms arrays by iterating over elements, applying a specified transformation, and producing a new array. 7. id, Jan 10, 2020 · How to add new fields to existing json Asked 5 years, 6 months ago Modified 5 years, 6 months ago Viewed 1k times MuleSoft Documentation SiteThis example iterates over an input array (["jose", "pedro", "mateo"]) to produce an array of DataWeave objects. Input: [ { "id": 12, "name": "sun", "age": 25 }, { "id" Jul 14, 2024 · DataWeave - Add additional item after iteration used for list Asked 10 months ago Modified 10 months ago Viewed 93 times May 21, 2019 · Learn how to create an array of numbers in DataWeave. Sep 21, 2022 · One such thing we often encounter is comparing two arrays and finding the differences. Learn more about MuleSoft here! IMPORTANT: The MuleSoft Community Forums have moved to the online Trailblazer Community. In this tutorial, you’ll learn some examples or use cases in which you can iterate through all the items in an Array to change them. Let's check out the different scenarios and see how we can achieve them. Similar to concat or + functions in other languages, in DW you can use ++ (plus plus), using { ( )} and { ( [ ])} parentheses object destructors with or without arrays. 2 runtime onwards) you may use the "repeat" function to do so. Transforming Complex XML Data Structures The XML data structure doesn’t support arrays. 0 output application/json --- { result: payload. result map ((item) -> { Labels: item. Curly braces “ {}” defines the object parentheses “ ( )” are used I am trying to change the value of field in element of array in json with condition: if value of "field1" equals "value1", then change value of field "changethisfield" to "new_value". The joinBy function takes an array as first argument and a string value that is used as separator. Feb 22, 2020 · I'm working on trying to get from the below source/target structures, using Dataweave 2. Output - "SELECT Name FROM Account WHERE Eq = true AND comp = true" In this tutorial, you’ll learn some examples or use cases in which you can iterate through all the items in an Array to change them. makes little sense to me. The function returns a new array that contains given value at the specified index. The input also includes the key magazine, which is ignored. the more familiar slice notation, arr[n to m]. If we want to include an element in an array based on a condition we can enclose the element between parenthesis (element) followed by an if statement. topics: May 12, 2023 · You can use the reduce () function to transform an array into an object. We will use the mapfunction to iterate over each customerin the array and create a new structure for each one. To ensure you keep your full activity history, please create a free Trailblazer account using the same email address as for your current MuleSoft forum account. The pain point May 13, 2024 · Go through each item in a array, one at a time. Now I was not quite sure to achieve the same , so I have taken a rudimentary approach of iterating over the array and literally recreating a new array with additional desired attributes at desired locations as below : %dw 2. If the condition of the if statement returns true the element is included in the array. There might be a more efficient way to combine the two loops into one. By creating a Trailblazer account, your content will migrate with your identity; your likes and upvotes will be preserved. Try putting them in an array. Strings (dw::core::Strings) This module contains helper functions for working with strings. Get started with DataWeave. Advance, Use Cases array, indexof, join 2 array, sum of values in array Manipulation On Array In Mule 4 In this tutorial we will demonstrate how can we do Manipulation On Array In Mule 4. 0 output application/json --- payload + {prepay: "Y", prepayRate: "ratePerLine"} NOTE : >> can be used to prepend an object to an array, example - {} >> [] Hope this helps! Best, Chetan Nov 14, 2022 · For you can add the line import * from dw::core::Arrays to the header of your DataWeave script. Sep 27, 2022 · The core of DataWeave is to transform data from one format to another format. For example, in Mule their event concept provides the context, adding inputs such as payload and vars, with their respective MIME type information. toArray toArray (@StreamCapable text: String): Array<String> Splits a String value into an Array of characters. In this tutorial, you will learn how to transform an input Object into an Array with the pluck function. Jan 2, 2023 · In this example, the DataWeave transformation extracts the “id”, “name”, and “street” fields from a nested object in the input payload. Use the Collection field for this purpose. Edited the transform message Please Create one variable before External call and set the default value as [] every time you are getting response in the payload, you need to store those records in the variable like %dw 2. . name ) >10 do something else null, but the expression I am using doesn't seem to be matching to what Mule 4 is expecting. Flatten Elements of Arrays DataWeave can flatten subarrays of an array and collections of key-value pairs within DataWeave objects, arrays, and subarrays. 0 In the case of XML, if you want to DataWeave Code in MuleSoft Applications Examples These examples show how to use DataWeave to extract and transform data in MuleSoft applications. [obj1, obj2] I am not sure if this is helpful, i dont fully understand the question. Hello Viewer This page explains about how an array of elements can be converted into single object Aug 18, 2018 · This post reviews how to accumulate a value into an array within a for-each loop. In the 4. I'm going to cover how to do this in Mule 4, where we don't have access to MEL. Here is what I currently have as an example: DataWeave filter Function: How to Filter Items in an Array DataWeave groupBy Function: How to Group Items from Arrays, Strings, or Objects DataWeave map Function: How to Iterate Through All Items in an Array The Problem For this experiment, we begin with a simple example that illustrates one approach to merging data from two sources. I will be discussing two of the methods among many in Dataweave 2. Before you begin to use DataWeave to code your own powerful and complex data transformations, you must understand the basic concepts of programming and the core features of functional programming languages. thanks in advance !! Apr 10, 2022 · If you don't know how to remove or add them with Dataweave. for example all first index from e MuleSoft Documentation SiteNote that if the operands of the relational operator belong to different types, DataWeave coerces the right-side operand to the type of the left-side operand. As {index: value} shows, each index from the input array becomes a key for an output object, and each value of the input array becomes the value of that object. As a MuleSoft developer, one must upskill one's expertise in DataWeave, which will help us create a full-fledged MuleSoft project. Dec 16, 2020 · I want to add an array attributes to an existing element dynamically. Is this possible? Feb 19, 2021 · Add Key to an Array if Element Exist using Dataweave Asked 4 years, 5 months ago Modified 4 years, 5 months ago Viewed 1k times If I add a "quantity" field so I also have to multiply by that field, like so: should I be multiplying the getSum function by the quantity field? You can use `reduce` function to create sums and add them to original payload. Request: { "contact": [ { " Oct 15, 2019 · DataWeave provides a mechanism that allows the insertion of attributes into an XML element using the “ @” symbol followed by key-value pairs. Examples illustrate its versatility, yielding structured outputs for different input formats. In addition, DataWeave natively supports namespaces, CData an xsi:types. Have a JSON Object which I need to map to an array of objects but can't get the structure right after many tries. Add a myInput DataWeave variable supplying some input data the script can read, and change the Mule payload variable in the body of the script to the DataWeave myInput variable. The input argument is an object and a lambda and the output must be an array. May 6, 2024 · Dataweave 2. 0? I am trying to do a condition around one of the Payload Data Elements like if sizeOf (payload [0]. In DataWeave, concatenation can be achieved by using the ++ function. To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Strings to the header of your DataWeave script. XML Format MIME type: application/xml ID: xml The XML data structure is mapped to DataWeave objects that can contain other objects, strings, or null values. 2. Read this tech article to learn how to transform the array data structure in DataWeave 2. My requirement was to put the additional key-value (the same for all objects) at index 1, so i've Extract Data DataWeave can select data from DataWeave objects and arrays, variables that store that data, and the output of DataWeave functions when that output is an array or object. Map Object Elements as an Array This DataWeave example uses the DataWeave map function to iterate through the object elements that match the key book. XML uses unbounded elements to represent collections, which are mapped to repeated keys in DataWeave objects. for example: I have 3 objects: {a : apple}, {b: banana}, {c: cat} If in the input we get an array like [ {a : apple}, {c: cat} ] we have to add the other object and send the output. Transforming an array of objects into a CSV file: For Each does not modify the current payload. Jul 3, 2023 · Is input 2 supposed to be an object? It uses array square brackets which doesn't make sense since it has key-value pairs. below is the sample request. 0 : Converting an Array of elements to Single Object and adding a new key-value pair. To use this module, we first need to import DataWeave code, for example, by adding the line import * from dw::core::Arrays to the header of your DataWeave script Please find below few important Array function Aug 21, 2020 · Add one more element to the object array inside for each loop in mule 3 Asked 4 years, 10 months ago Modified 4 years, 9 months ago Viewed 2k times @Vinay oboyENREj (Customer) You can use + or << to append an object to an array. In the expression 123 > "12", DataWeave coerces the Merge Fields from Separate Objects The DataWeave examples merge fields from separate input arrays. NOTE: To prepend to can do obj >> arr Hope this helps! Reference : DataWeave Operators | MuleSoft Documentation Best, Chetan DataWeave expression allows developer to apply a Conditional Elements based on a conditional expression. 0 makes it easier to define an array with a range (“<start-index> to <end-index>”). Once we have transformed our string to an array we can use all functions that work on arrays. For non-Java collections, such as XML or JSON, use a DataWeave expression to split data. In the following example, the Collection field in For Each is set to iterate over an array stored in payload. Append data to Array object in dataweave 2. Jul 25, 2023 · In this article, we will explore two common scenarios related to arrays in DataWeave and understand how to find common and non-common elements between two arrays. Mar 28, 2024 · This blog explains to process to converting Object Data to Array and vice-versa. JSON supports String, Boolean, Number, Null, Object, and Array types. For example, in the expression "123" > 12 DataWeave coerces 12 (a Number type) to "12" (a String type) and compares each String value lexicographically. We'll also learn different ways of handling null values for our fields. value Aug 29, 2021 · How to add data based on particular column in an array using dataweave 2. Jan 27, 2024 · There are different methods to update Specific Key-Value pair in Object (Simple or Nested). The following example creates an array of empty JSON objects: Feb 10, 2022 · February 10, 2022 DataWeave Delight: Turn string value into array of characters A string value can be seen as an array of characters and if we want to transform our string value to an array we can use the toArray function in the dw::util::Coercions module. 4. We’ll show you how to write the prefix and infix notations, as well as the use of an explicit lambda and the dollar-sign syntax. Mar 1, 2022 · I have an input payload (json array) that needs to be enriched with a key-value in a specific index. DataWeave allows us to read data from various formats, such as JSON, text, CSV, and XML. id: ++ payload }` JSON Format MIME type: application/json ID: json In the JSON data format, values map one-to-one with DataWeave values. Others return indices of a string. Can anyone help me with this scenario Thanks in Advance DataWeave 2 Chetan MuleSoft Documentation SiteTo use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Arrays to the header of your DataWeave script. 0 output application/json --- content : (payload. Attached json input Input json Mar 2, 2023 · how to add two element of of array in dataweave Asked 2 years, 5 months ago Modified 2 years, 5 months ago Viewed 1k times Dataweave 1. The DataWeave example shows how to pass the XML attributes in the NameDetail field of the input source payload to the TargetName field of the XML output. its Object Constructor which using to create instance of an Object, in other words it convert the array to Object Yes we can do with reduce also, Please check below dataweave In this tutorial, you’ll learn some examples or use cases in which you can filter an Array to only contain the items that pass a certain condition. The output payload is the same as the input. 0 Asked 2 years, 4 months ago Modified 2 years, 4 months ago Viewed 3k times Each array of objects will have more key-value pairs and multiple inner arrays not only xxx & a [] need to update the city without doing 1:1 mapping on other keys value pairs. The reduce function is about as close as we get to a general-purpose looping tool in DataWeave.
vsl kywbda qfxup wwmm blohubdi gssp fbuwft sca out wtxoh