Skip to Content

Json structure having fields to replace the field values abc to abc:def:67:dfdf in SAP HCI

Dear Experts,

Please help me Groovy Script to Find and Replace Text the field values.

E.g:

Input Payload

{ "userName": "",

"name":

{ "givenName": "

", "familyName": "",

"middleName": "",

},

After Conversion Output should like this:

{ "user:Name:123333": " ",

"na:m:e":

{ "$givenName": "

", "$family:Name": "",

"middle:Name": "",

}

Please help me to find and replace a field names.

i have tried this code but i am getting only one i need to replace multiple fileds

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

def Message processData(Message message) {
def msgBodyOriginal = message.getBody(java.lang.String) as String
def msgBodyModified = msgBodyOriginal.replace('userName', 'user:Name:123333')
message.setBody(msgBodyModified)
return message
}

Regards,

Naveen

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    Posted on Oct 12, 2018 at 07:58 AM

    Please try putting below

    \$givenName

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 11, 2018 at 05:19 PM

    Try this -

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

    def Message processData(Message message) {
    def msgBodyOriginal = message.getBody(java.lang.String) as String
    msgBodyOriginal.replace('userName', 'user:Name:123333')

    msgBodyOriginal.replace('name', 'na:m:e')

    msgBodyOriginal.replace('givenName', '$givenName')

    msgBodyOriginal.replace('familyName', '$family:Name')

    msgBodyOriginal.replace('middleName', 'middle:Name')
    message.setBody(msgBodyOriginal)
    return message;
    }

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Apu,

      Thanks for replying.

      I have tried this code not working..

      Then i have tried below this code working fine

      import com.sap.gateway.ip.core.customdev.util.Message;
      import java.util.HashMap;
      
      
      def Message processData(Message message) {
      
      
      def msgBodyOriginal = message.getBody(java.lang.String) as String
      
      
      def msgBodyModified = msgBodyOriginal.replaceAll('userName','user:Name:123333')
      def msgBodyModified1 = msgBodyModified.replaceAll('givenName', '$givenName')
      def msgBodyModified2 = msgBodyModified1.replaceAll('familyName',  '$family:Name')
      message.setBody(msgBodyModified2)
      return message;
      }
      But 'givenName' to '$givenName' i am not able to convert,I am getting this error:
      .java.lang.Exception: java.lang.IllegalArgumentException: Illegal group reference@ line 9 in RemoveBracket.groovy
      Please help me to pass $ symbol along with fieldname.
      Regards,
      Naveen

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.