<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Groovy script for XML to JSON conversion in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165686#M4548986</link>
    <description>&lt;P&gt;Thank you, Bibhu - your answer is a valid one and it worked for me after I removed the "Ccript Function" as suggested by Morten, but unfortunately I cannot accept 2 answers.&lt;/P&gt;&lt;P&gt;Thanks again for your time and support.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anirban&lt;/P&gt;</description>
    <pubDate>Thu, 09 Apr 2020 13:06:10 GMT</pubDate>
    <dc:creator>anirban_pwc</dc:creator>
    <dc:date>2020-04-09T13:06:10Z</dc:date>
    <item>
      <title>Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaq-p/12165678</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
  &lt;P&gt;I need to use Groovy script to overcome the shortcomings of the standard XML to JSON converter on the Cloud Platform Integration.&lt;/P&gt;
  &lt;P&gt;Here is the output JSON format I need to get:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;{
"PO": "test21",
"Items": [{
"Id": "000010",
"Product": "XXXXX",
"Quantity": 5,
"Discount_Percent": 0,
"Unit_Price": 0,
"Pharmaceutical": false,
}]
}&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;But this is what I received out of the converter:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;{
"PO": "test21",
"Items": [{
"Id": "000010",
"Product": "XXXXX",
"Quantity": "5.000",
"Discount_Percent": "0.000",
"Unit_Price": "",
"Pharmaceutical": "false",
}]
}&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I'm not a Groovy expert and tried utilizing the one provided in the SAP note to replace "false" with false using the following groovy but that didn't work:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
String output = body.replaceAll("\"false\"", "\$1");
message.setBody(output);
return message;
}
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Can someone please help with a groovy script that will work for me?&lt;/P&gt;
  &lt;P&gt;Thanks for your time.&lt;/P&gt;
  &lt;P&gt;Anirban&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 21:00:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaq-p/12165678</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2020-04-08T21:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165679#M4548979</link>
      <description>&lt;P&gt;HI,&lt;BR /&gt;&lt;BR /&gt;Please check your regular expression and change it as given below :-&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;String output = body.replaceAll("\"(false)\"", "\$1");&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;"( )" =&amp;gt;  Groups multiple tokens together and creates a capture group for extracting a sub string.&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I tried the same but i didn't get any error.&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1801466-capture1.png" /&gt;&lt;/P&gt;&lt;P&gt;Input : -&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1801467-capture2.png" /&gt;&lt;/P&gt;&lt;P&gt;Output:
&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1801468-capture3.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;As already shared by &lt;SPAN class="mention-scrubbed"&gt;7a519509aed84a2c9e6f627841825b5a&lt;/SPAN&gt;, that's the ideal way to typecast a given string to respective type.&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;Bibhu&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 21:52:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165679#M4548979</guid>
      <dc:creator>ibibhu</dc:creator>
      <dc:date>2020-04-08T21:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165680#M4548980</link>
      <description>&lt;P&gt;Hi Anirban&lt;/P&gt;&lt;P&gt;How this conversion should work is not completely clear. Might Unit_Price contain a price, or is it always empty and should be replaced with zero? And can Quantity and Discount_Percent have decimals, that should be kept? Or do you want to throw away everything after the decimal separator?&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Morten&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 21:59:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165680#M4548980</guid>
      <dc:creator>MortenWittrock</dc:creator>
      <dc:date>2020-04-08T21:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165681#M4548981</link>
      <description>&lt;P&gt;Hi Bibhu,&lt;/P&gt;&lt;P&gt;I received the same error as before:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1798475-21.png" /&gt;&lt;/P&gt;&lt;P&gt;Here is the updated code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import com.sap.gateway.ip.core.customdev.util.Message;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
String output = body.replaceAll("\"(false)\"", "\$1");
message.setBody(output);
return message;
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 22:05:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165681#M4548981</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2020-04-08T22:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165682#M4548982</link>
      <description>&lt;P&gt;Hi Morten,&lt;/P&gt;&lt;P&gt;The Unit_Price will have a decimal value but the Discount_Percent and Quantity are integers. Sorry for the confusion.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anirban &lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 22:08:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165682#M4548982</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2020-04-08T22:08:26Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165683#M4548983</link>
      <description>&lt;P&gt;Hi Anirban&lt;/P&gt;&lt;P&gt;Here's a Groovy script that cleans up the JSON. It does so without using regular expressions, and it only updates the values, that need to be updated.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def Message processData(Message message) {

    def json = message.getBody(java.lang.String)
    def jsonSlurper = new JsonSlurper()
    def object = jsonSlurper.parseText(json)

    object.Items.each { item -&amp;gt; 
        // Convert Pharmaceutical to a boolean value
        item.Pharmaceutical = (item.Pharmaceutical == 'false' ? false : true)
        // Convert Quantity to an integer
        item.Quantity = item.Quantity.toDouble().intValue()
        // Convert Discount_Percent to an integer
        item.Discount_Percent = item.Discount_Percent.toDouble().intValue()
        // If Unit_Price is empty, replace with zero, otherwise convert to double
        if (item.Unit_Price == '') {
            item.Unit_Price = 0
        } else {
            item.Unit_Price = item.Unit_Price.toDouble()
        }
    }

    message.setBody(JsonOutput.toJson(object))
    return message

}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When you add the script to a Script step, do &lt;EM&gt;not&lt;/EM&gt; enter anything into the Script Function field. That field has a specific purpose, which is not describing what the script does.&lt;/P&gt;&lt;P&gt;Given your input, it produces the following output:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1800452-output.png" /&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Morten&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 22:42:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165683#M4548983</guid>
      <dc:creator>MortenWittrock</dc:creator>
      <dc:date>2020-04-08T22:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165684#M4548984</link>
      <description>&lt;P&gt;Thanks a lot, Morten for the detailed script.&lt;/P&gt;&lt;P&gt;But pardon my ignorance as I'm absolutely green on groovy, I just copied your script and saved with a ".groovy" extension and added to the iFlow. But I'm still getting a generic error:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1798477-21.png" /&gt;&lt;/P&gt;&lt;P&gt;I'm sure missing a fundamental step in the process. Do I need to install any JAR file or something?&lt;/P&gt;&lt;P&gt;Additionally, if I may add a follow up question for the script - what if I want to clean another element at the root level, for example, if the "PO" is an integer, should I address it as &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;object.&lt;STRONG&gt;root&lt;/STRONG&gt;.each { item -&amp;gt;&lt;BR /&gt;item.PO = item.Quantity.toDouble().intValue()&lt;BR /&gt;}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you again for your time and support.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anirban&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 00:32:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165684#M4548984</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2020-04-09T00:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165685#M4548985</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;amallick&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;As for the error, please note this sentence in the error message: "replaces double quotes from Boolean data types". That sentence does not occur in my script, so I'll take a wild guess that you entered it into the Script Function field, under the assumption that that's where you document what the script does. And this is now when I urge you to re-read my answer, particularly this bit:&lt;BR /&gt;&lt;BR /&gt;"When you add the script to a Script step, do &lt;EM&gt;not&lt;/EM&gt; enter anything into the Script Function field. That field has a specific purpose, which is not describing what the script does."&lt;BR /&gt;&lt;BR /&gt;As for your follow-up question about cleaning up another value, that's now your job to experiment with. As I often say in here, it's a very bad idea to copy-paste code you don't understand into your solution.&lt;BR /&gt;&lt;BR /&gt;So your next step is to study the script, and experiment with modifying it. Take a look at the JsonSlurper class in particular, since it does most of the work.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Morten&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 09:57:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165685#M4548985</guid>
      <dc:creator>MortenWittrock</dc:creator>
      <dc:date>2020-04-09T09:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165686#M4548986</link>
      <description>&lt;P&gt;Thank you, Bibhu - your answer is a valid one and it worked for me after I removed the "Ccript Function" as suggested by Morten, but unfortunately I cannot accept 2 answers.&lt;/P&gt;&lt;P&gt;Thanks again for your time and support.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anirban&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 13:06:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165686#M4548986</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2020-04-09T13:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165687#M4548987</link>
      <description>&lt;P&gt;Thank you, Morten for drawing my attention to the "Script Function" and after removing the description, the script worked for me and I do understand the code, but wondering if you can help me understanding how I can work with the root level elements, like the "PO" element for example.&lt;/P&gt;&lt;P&gt;Thanks a ton,&lt;/P&gt;&lt;P&gt;Anirban&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 13:08:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165687#M4548987</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2020-04-09T13:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy script for XML to JSON conversion</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165688#M4548988</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;amallick&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Look at how the code works, and improvise from there. You can get at the PO key with object.PO.&lt;/P&gt;&lt;P&gt;And now you &lt;EM&gt;really&lt;/EM&gt; need to experiment and learn by yourself. Take the hint, dude.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Morten&lt;/P&gt;</description>
      <pubDate>Thu, 09 Apr 2020 14:06:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-script-for-xml-to-json-conversion/qaa-p/12165688#M4548988</guid>
      <dc:creator>MortenWittrock</dc:creator>
      <dc:date>2020-04-09T14:06:03Z</dc:date>
    </item>
  </channel>
</rss>

