maahutch
3 years agoNew Contributor
Converting an array to a denormalized table
This seems like it should be simple but I’m still relatively new to snaplogic and just can’t figure it out. The json coming into my Mapper snap is formatted like this:
{"record1": {"id" : "1",
"subject":{
"subject_id": ["a", "b"]
}
},
"record2": {"id" : "2",
"subject":{
"subject_id": ["b", "c"]
}
}
}
Which I’d like to convert to a table like this:
id | subject_id
________________
1 | a
1 | b
2 | b
2 | c
I’ve been trying something like this based on some other posts I saw here but I don’t know what I’m doing
$.keys().filter(k => k.startsWith("$subject_id")).map(k => k.substring("$subject_id".length)).map(i => {"id": $.get("id"+i), "subject_id": $.get("$subject_id"+i)}
Can someone explain to a beginner, how to perform this type of transformation?