I have this JSON object
"actos-evaluacion" : [
{
"subject" : < id-subject>,
"name" : < name>,
"marks" : ["<student_id>:< mark>","< student_id>:< mark>",
"<student_id>:< mark>"] },
...
{
"subject" : < id-subject>,
"name" : < name>,
"marks" : ["<student_id>:< mark>","< student_id>:< mark>",
"<student_id>:< mark>"] },
] }
And I want to convert it into this Map, so there will be more Maps than JSON, because each JSON will split into different maps depending on the number of students it has.
{
id_subject :
name :
student_id :
mark :
}
I've tried putting a foreach(json) where subject_id and name where stored in variables. Then set_payload with the list and another foreach for the elements of the list but I don't know how to recover the variables into a Map operator to join them all
A DataWeave script can generate the output. I had to make some assumptions of what the input and output actually look to illustrate the method to loop over the internal array inside each element of the first array. You can adapt the method if the samples are not exactly what you use. My script generates an array of DataWeave objects that are equivalent to a Java Map.
Input:
{
"actos-evaluacion" : [
{
"subject" : "id1",
"name": "name1",
"marks": [ "student1:mark1", "student2:mark2", "student1:mark3"]
},
{
"subject" : "id2",
"name" : "name2",
"marks": [ "student1:mark1", "student2:mark2", "student1:mark3"]
}
]
}
DataWeave script:
%dw 2.0
output application/json
---
payload."actos-evaluacion" flatMap ((item, index) ->
item.marks map ((subject, index2) -> {
id_subject: item.subject,
name: item.name,
student_id: (subject splitBy ":") [0],
mark: (subject splitBy ":")[1]
})
)
Output:
[
{
"id_subject": "id1",
"name": "name1",
"student_id": "student1",
"mark": "mark1"
},
{
"id_subject": "id1",
"name": "name1",
"student_id": "student2",
"mark": "mark2"
},
{
"id_subject": "id1",
"name": "name1",
"student_id": "student1",
"mark": "mark3"
},
{
"id_subject": "id2",
"name": "name2",
"student_id": "student1",
"mark": "mark1"
},
{
"id_subject": "id2",
"name": "name2",
"student_id": "student2",
"mark": "mark2"
},
{
"id_subject": "id2",
"name": "name2",
"student_id": "student1",
"mark": "mark3"
}
]