SAP CPI How to publish JSON Content in a Header using Groovy

I wanted to get a list of all the Values from the following payload :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "Root":[
    {
      "Node":{
        "Value": "AAA"
      },
      "Node":{
        "Value": "BBB"
      },
      "Node":{
        "Value": "CCC"
      }
    }
  ]
}

To extract data from a JSON payload and store it in a message header use the following :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  import com.sap.gateway.ip.core.customdev.util.Message;
  import groovy.json.*;
  import java.util.HashMap;
    
  def Message processData(Message message) {

    //Body 
    def body = message.getBody(String);
    
    // Get List of Values
    def payload = new JsonSlurper().parseText(body)
    def values = payload.Root[0].Node.collect { it.Value }
    def result = values.join(',')

    //Headers 
    def map = message.getHeaders();
    message.setHeader("MyList", result);

    return message;
    }