on 08-03-2020 6:44 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.