Skip to Content
0

How to remove root element in JSON after conversion in groovy script

Nov 23, 2017 at 03:00 PM

334

avatar image

Hi Experts,

How can we remove root element in JSON after conversion using groovy script in HCI.

After JSON Converter, it will become:

{
  "MT_Request": {
    "CORPID": "API1",
    "USERID": "User2",
    "DEBITACC": "000405002345",
    "CREDITACC": "000405001223",
    "IFSC": "SA12345 ",
    "AMOUNT": "7",
    "CURRENCY": "INR",
    "TXNTYPE": "RWS",
    "AGGRID": "1234",
    "PAYEENAME": "party",
    "UNIQUEID": "UNIQUE_ID",
    "AGGRNAME": "ABC",
    "URN": "Test1",
    "REMARKS": "Test Payload1"
  }
}

So, I want the output as below :

{
  "CORPID": "API1",
  "USERID": "User2",
  "DEBITACC": "000405002345",
  "CREDITACC": "000405001223",
  "IFSC": "SA12345 ",
  "AMOUNT": "7",
  "CURRENCY": "INR",
  "TXNTYPE": "RWS",
  "AGGRID": "1234",
  "PAYEENAME": "party",
  "UNIQUEID": "UNIQUE_ID",
  "AGGRNAME": "ABC",
  "URN": "Test1",
  "REMARKS": "Test Payload1"
}

When i am using one groovy script its working fine but all fields are shuffling.

Used Groovy script:

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

def Message processData(Messagemessage){
    def body =message.getBody(java.lang.String)asString
    def jsonParser =new JsonSlurper()
    def jsonObject = jsonParser.parseText(body)message.setBody(JsonOutput.toJson(jsonObject["MT_Request"]))returnmessage;}

I am getting shuffled output as below .

{
  "CORPID": "API1",
  "AGGRNAME": "ABC",
  "PAYEENAME": "party",
  "URN": "Test1",
  "CREDITACC": "000405001223",
  "DEBITACC": "000405002345",
  "REMARKS": "Test Payload1",
  "AMOUNT": "7",
  "UNIQUEID": "UNIQUE_ID ",
  "IFSC": "SA12345",
  "AGGRID": "1234",
  "TXNTYPE": "RWS",
  "CURRENCY": "INR",
  "USERID": "User2"
}

Thanks and Regards,

Md.Thouheed

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Mohammad Thouheed Dec 09, 2017 at 02:07 PM
0

Hi Sriprasad,

Thanks for quick response, i found the other solution also it's worked successfully.

Just selected the suppress JSON Root Element check box.

Thanks and Regards,

Md.Thouheed


1.jpg (56.0 kB)
Share
10 |10000 characters needed characters left characters exceeded
Sriprasad Shivaram Bhat
Nov 24, 2017 at 07:18 AM
0

Hello Md,

Its known behavior of JSON parser.

Use below code it should work ( if you just want to remove header node starting with MT*).

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')+15);
  
	def json_to_str=body.substring(1,body.length()- 2);  
	message.setBody(json_to_str);
	
	return message;
}

Regards,

Sriprasad Shivaram Bhat

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Shivaram,

Thanks a lot for given quick reply, but i'm getting below error.

Then i tried below script to substring, then also its not working

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String;
	body = body.substring(body.indexOf(1,4);
  
	def json_to_str=body.substring(1,body.length()-2);  
	message.setBody(json_to_str);
    return message;
}

Thanks and Regards,

Md.Thouheed

capture.jpg (40.4 kB)
capture1.jpg (42.4 kB)
0

Hello Md,

Its working fine for me.Please use the content of

script.txt

Regards,

Sriprasad Shivaram Bhat

script.txt (314 B)
0