0

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

aled
  • 21,330
  • 3
  • 27
  • 34
twoo
  • 3
  • 1
  • 1
    Please provide a correct input with actual values and an expected output with values that match the provided input. Also try to use correct code formatting so the samples are more readable and usable. – aled Nov 12 '22 at 21:20

1 Answers1

0

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"
  }
]
aled
  • 21,330
  • 3
  • 27
  • 34
  • Hey, I have dw 1.0, how could I change the flatMap with that version? – twoo Nov 13 '22 at 11:30
  • You should have mentioned that in the question. flatMap() doesn't exist in DataWeave 1.0 but you can replace it with a normal `map` and enclose it's result with the `flatten` operation. – aled Nov 13 '22 at 11:54