Mule 4/DW 2.0: Iterate array inside objects of arrays

Mule4NY

Here is the sample input JSON: *

[
  {
    "animation_production_studios": [],
    "audio": [
      "English",
      "Japanese"
    ],
    "videos": [
      {
        "DASH": true,
        "aips": [
          400,
          824,
          1191
        ],
      },
      {
        "DASH": true,
        "aips": [
          401,
          825,
          1192
        ],
       }
]
 ]
  },
  {
    "animation_production_studios": ["Studio Chizu"],
    "audio": [
      "English",
      "Japanese"
    ],
    "videos": [
      {
        "DASH": true,
        "aips": [
          403,
          826,
          1193
        ],
      },
      {
        "DASH": true,
        "aips": [
          404,
          827,
          1194
        ],
       }
]
 ]
  }
]

*

Here is the expected output (to be able to insert into a db as below):

animation_production_studios    audio           videos_dash  videos_aips
------------------------------------------------------------------------------------
Null                       English, Japanese    true         400, 824, 1191
Null                       English, Japanese    true         401, 825, 1192
Studio Chizu               English, Japanese    true         402, 826, 1193
Studio Chizu               English, Japanese    true         403, 827, 1194

Here is the DW%2.0 code I tried:

payload map(item, index) -> {
   (item.videos map(viditem, vidindex)) -> {
        videos_aips: (viditem.videos map ((vitem, vindex) -> 
        vitem.aips reduce ((vi, vacc) -> vacc + vi )))[0],
        show_id: item."show_id",
        audio: (payload map ((aitem, aindex) -> item.audio                  
            reduce ((i, acc) -> acc ++ "," ++ i )))[0] 
   }
}

Throwing an error:

(item.videos map(viditem, vidindex)) -> {
                        ^
Invalid input ',', expected ')' for the enclosed expression.
oim

Please see below

%dw 2.0
output application/json

var inputData = read('{
  "data": [
    {
      "animation_production_studios": [],
      "audio": [
        "English",
        "Japanese"
      ],
      "videos": [
        {
          "DASH": true,
          "aips": [
            400,
            824,
            1191
          ]
        },
        {
          "DASH": true,
          "aips": [
            401,
            825,
            1192
          ]
        }
      ]
    },
    {
      "animation_production_studios": [
        "Studio Chizu"
      ],
      "audio": [
        "English",
        "Japanese"
      ],
      "videos": [
        {
          "DASH": true,
          "aips": [
            403,
            826,
            1193
          ]
        },
        {
          "DASH": true,
          "aips": [
            404,
            827,
            1194
          ]
        }
      ]
    }
  ]
}', 'application/json')

fun concatArray(objArray=[], separator=",") = 
    if (sizeOf(objArray) == 0) 
        "Null"
    else
        objArray joinBy separator

---
flatten(inputData.data map(item) -> 
  item.videos map (vidItem) -> {
    "animation_production_studios" : concatArray(item.animation_production_studios, ","),
    "audio" : concatArray(item.audio, ","),
    "videos_aips" : concatArray(vidItem.aips, ","),
    "videos_dash" : vidItem.DASH
})

This will result to:

[
  {
    "animation_production_studios": "Null",
    "audio": "English,Japanese",
    "videos_aips": "400,824,1191",
    "videos_dash": true
  },
  {
    "animation_production_studios": "Null",
    "audio": "English,Japanese",
    "videos_aips": "401,825,1192",
    "videos_dash": true
  },
  {
    "animation_production_studios": "Studio Chizu",
    "audio": "English,Japanese",
    "videos_aips": "403,826,1193",
    "videos_dash": true
  },
  {
    "animation_production_studios": "Studio Chizu",
    "audio": "English,Japanese",
    "videos_aips": "404,827,1194",
    "videos_dash": true
  }
]

Note here that I have tried to correct your input to make it valid and put in under "data". Idea here is that you will have outer loop (iterating over parent array) through map and have an inner loop (iterating over videos). There might be a more efficient way to combine the two loops into one. I will update if I find something.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterate over and count objects inside objects inside arrays

php iterate through array of objects inside an array

Iterate over array nested in inside an array of objects?

Iterate through 2 arrays containing objects and return the values of a second array using the first arrays keys

What's the efficient way to iterate through 2 arrays of Objects and return a single array of objects?

Iterate array of objects inside node using Python

Multiple arrays of objects inside array of objects in MongoDB

Mule 4 DataWeave 2 Mapping of Arrays

Iterate an Array of Objects in Ionic 2 & AngularJS 2

Merging multiple arrays inside an objects into a single array

PHP Array of Objects with arrays inside. Merge these

How to sort different arrays of objects inside an array

Filter an Array containing several arrays with objects inside

Angular 2+ Iterate over array of arrays

iterate through array of objects and change apperence of another array inside

.map() to iterate an arrays of objects

Iterate Array Inside of Array Angular 4

How to iterate with two arrays inside an array using the filter() method

Iterate over two arrays of objects and create new array object

How to iterate an array of arrays of objects with *ngFor in Angular 14?

Iterate over array of objects and concatenate their attributes which are arrays themselves

Using for loops only to iterate through an array of objects with other arrays

MongoDB update inside 2 nested arrays of objects

VueJS 2 - Watch properties inside arrays and/or objects

How to continuously iterate in multiple arrays in mule dataweave?

How to insert arrays into objects that are inside an array? For ng2-charts / Charts.js

Iterate over object with some keys and array of objects inside

Run `every` method on array of objects inside arrays of objects

Merging an array of objects and also merging the arrays inside the objects with the same key