cancel
Showing results for 
Search instead for 
Did you mean: 

How to Split a Row into Multiple Rows

machinenis
New Contributor

Please help to split a Row/Document into multiple Rows/Documents.
If my JSON Input Looks like below in the mapper Snap and I need to split this into multiple documents as shown in the OUPUT
[
{

           "School_Name": Elementary school,
          "County_Name":Wake,
           "Grade": 5
           "Student_ID1": 41,
           "Student_ID2": 55,
           "Student_ID3": 66,
           "Student_Name1": "Scott",
           "Student_Name2": "Lisa",
           "Student_Name3": "Thomas",
           "Age1": "11",
           "Age2": "12",
           "Age3": "13",
           }

]

OUTPUT:

School_Name | County_Name | Grade | Student_ID | Student_Name | Age
Elementary school | Wake | 5 | 41 | Scott | 11
Elementary school | Wake | 5 | 55 | Lisa | 12
Elementary school | Wake | 5 | 66 | Thomas | 13

1 REPLY 1

ptaylor
Employee
Employee

Hi. See the attached pipeline:

Community7305_2020_05_08.slp (5.1 KB)

The crux is using an expression like the following. Note that in the filter and first map callback, you can use any of the key prefixes that have numeric suffixes: “Student_ID”, “Student_Name”, or “Age”; I chose “Age” since it’s short.

$.keys()
.filter(k => k.startsWith("Age"))
.map(k => k.substring("Age".length))
.map(i => {"Student_ID": $.get("Student_ID"+i), 
           "Student_Name": $.get("Student_Name"+i), 
           "Age": $.get("Age"+i)})