3
votes

Scenario : Given an input as below sampleArray , I will like to group all the students having a specific teacher.

In DataWeave we have a method groupBy that allows us to group arrays specifying a string key. But here since item.studentsMarks.subjectTeacher returns an array I am getting an error specified below.

Can anyone please help. Thanks in Advance.

Attached below is expected output.

Sample Input:

var sampleArray = [
    {
        "studentName" : "ABC",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "50",
                "subjectTeacher" : "teacher_1"
            },
            {
                "subject" : "science",
                "marks" : "30",
                "subjectTeacher" : "teacher_3"
            }
        ]
    },
    {
        "studentName" : "XYZ",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "90",
                "subjectTeacher" : "teacher_1"

            },
            {
                "subject" : "arts",
                "marks" : "50", 
                "subjectTeacher" : "teacher_2"
            }
        ]
    }
]


Code tried:

payload groupBy ((item, index) -> item.studentsMarks.subjectTeacher)

Error:

Cannot coerce Array to String

Expected Output :

[
    
    "teacher_1" : [{
            "studentName" : "ABC",  
            "studentsMarks" : [
                {
                    "subject" : "maths",
                    "marks" : "50",
                    "subjectTeacher" : "teacher_1"
                },
                {
                    "subject" : "science",
                    "marks" : "30",
                    "subjectTeacher" : "teacher_3"
                }
            ]
        },    
        {
                "studentName" : "XYZ",  
                "studentsMarks" : [
                    {
                        "subject" : "maths",
                        "marks" : "90",
                        "subjectTeacher" : "teacher_1"
        
                    },
                    {
                        "subject" : "arts",
                        "marks" : "50", 
                        "subjectTeacher" : "teacher_2"
                    }
                ]
           }
    ],
    "teacher_2" : [
        
                {
                "studentName" : "XYZ",  
                "studentsMarks" : [
                    {
                        "subject" : "maths",
                        "marks" : "90",
                        "subjectTeacher" : "teacher_1"
        
                    },
                    {
                        "subject" : "arts",
                        "marks" : "50", 
                        "subjectTeacher" : "teacher_2"
                    }
                ]
           }
    ],
    "teacher_3" : [
        
{
            "studentName" : "ABC",  
            "studentsMarks" : [
                {
                    "subject" : "maths",
                    "marks" : "50",
                    "subjectTeacher" : "teacher_1"
                },
                {
                    "subject" : "science",
                    "marks" : "30",
                    "subjectTeacher" : "teacher_3"
                }
            ]
        }
    ]
        
]

1
What is the output like? BTW the issue is with this experession item.studentsMarks.subjectTeacher because item.studentsMarks is an array--as such the next .subjectTeacher is a selector very similar to .*subjectTeacher, which returns an array, hence why you get the error you get.George
@George Hi, I have attached the expected output. I understood the issue but am looking for a way to implement the solution using groupBy. please let me know if you can find a suitable approach. Many thanksBibek Kr. Bazaz

1 Answers

4
votes

Here's a pretty quick way to get it done. It entails more than just a groupBy though:

%dw 2.0
output application/json

var sampleArray = [
    {
        "studentName" : "ABC",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "50",
                "subjectTeacher" : "teacher_1"
            },
            {
                "subject" : "science",
                "marks" : "30",
                "subjectTeacher" : "teacher_3"
            }
        ]
    },
    {
        "studentName" : "XYZ",  
        "studentsMarks" : [
            {
                "subject" : "maths",
                "marks" : "90",
                "subjectTeacher" : "teacher_1"

            },
            {
                "subject" : "arts",
                "marks" : "50", 
                "subjectTeacher" : "teacher_2"
            }
        ]
    }
]
---
// Get list of all teachers
sampleArray..*subjectTeacher
// Change the collection to a set 
distinctBy $ 
// Iterate over every single teacher and create an object where
// the field is the teacher while the value is a collection
// containing all students where they attend a class for the teacher.
map {
    ($): (sampleArray dw::core::Arrays::partition (e) -> e..*subjectTeacher contains $).success
}