cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script to format JSON

anirban_pwc
Participant
0 Kudos

Hello Experts,

I have a JSON payload in following format...

{
    "root": {
        "teams": [
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "422511",
                "complete": "false"
            },
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "421802",
                "complete": "false"
            }
        ]
    }
}

But I need it in following format:

 [
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "422511",
                "complete": "false"
            },
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "421802",
                "complete": "false"
            }
 ]

To achieve this, I'm using the following Groovy script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
    
    def body = message.getBody();
    body = body.substring(body.indexOf('\n')+19);
  
    def json_to_str=body.substring(1,body.length()-3);  
    message.setBody(json_to_str);
    
    return message;
}

But I'm receiving error messages like:

Error at line: 7
***
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.io.ByteArrayInputStream.indexOf() is applicable for argument types: (String) values: [
]
Possible solutions: inject(groovy.lang.Closure), findIndexOf(groovy.lang.Closure)
    at com.groovyide.ExecutorService$1.run(ExecutorService.java:84)

Could someone please suggest what I'm doing wrong here?

Thank you for your support.

Regards,

Anirban Mallick

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor

Hello Anirban,

Adding to Yoga reply below would do the needfull I gues.

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def Message processData(Message message) {
def json = message.getBody(java.lang.String)
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(json)
 
def builder = new JsonBuilder()
builder  object.root.teams.each { 
     item -> if(item.complete == 'false'){
             item.remove('complete')
            } 
    }
message.setBody(builder.toPrettyString())
return message;
}

Regards,

Sriprasad Shivaram Bhat

anirban_pwc
Participant
0 Kudos

Thank you both yoganandamuthaiah and sriprasadshivaramabhat .

Really appreciate your help.

Regards,

Anirban

0 Kudos
{
    "root": [{
        "teams": [
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "422511",
                "complete": "false"
            },
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "421802",
                "complete": "false"
            }
        ]
    } ]} 

For two array data this code is not working ? please help

Answers (1)

Answers (1)

yogananda
Product and Topic Expert
Product and Topic Expert

Hi amallick

check this two articles if that helps to your questions for now quickly..

https://abusinesstech.com/learn-sap-cpi-groovy-mapping-json-to-xml.html

https://int4.com/groovy-scripts-native-handling-of-json

anirban_pwc
Participant
0 Kudos

Hello yoganandamuthaiah,

Thank you for the references. I'm very close to accomplish the desired output with the following script:

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper


def Message processData(Message message) {
def json = message.getBody(java.lang.String)
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(json)
 
def builder = new JsonBuilder()
builder  object.root.teams.each { 
     item -> 
    }

message.setBody(builder.toPrettyString())
return message;
}

The last piece remaining is removing a JSON element from the final output, in this case the element "complete". I tried using "return" inside the each loop as follows but that didn't remove the element from the output:

if(item.complete == 'false'){
    return
} 

Here is what my current output looks like...

[
    {
        "VisitFrequencyPerYear": "0",
        "UPI": "422511",
        "complete": "false"
    },
    {
        "VisitFrequencyPerYear": "1",
        "UPI": "421802",
        "complete": "false"
    }
]

Could you please suggest anything?

Thank you again,

Anirban