AnsweredAssumed Answered

This Custom Script runs in my system but not in Boomi. Please help

Question asked by balpreet.singh102427 on Feb 9, 2018

Script: Converts XML to JSON and JSON to XML.

import java.util.Properties;
import java.io.InputStream;
import groovy.json.*;
import groovy.xml.*;

 


for( int i = 0; i < dataContext.getDataCount(); i++ ) {
  InputStream is = dataContext.getStream(i);
  String inString = streamToString(is);
  Properties props = dataContext.getProperties(i);
  byte[] outBytes;
  if (is.toString() =~ '<.*')
    {
    def xml = new XmlParser().parseText(inString);
    def jsonWriter = new StringWriter();
    def jsonBuild = new JsonBuilder(jsonBuilder(xml));
    outBytes = jsonBuild.toString().getBytes("UTF-8");
    }
    else
    {
    def json = new JsonSlurper().parseText(inString);
    def xmlWriter = new StringWriter();
    def xmlBuild = new MarkupBuilder(xmlWriter);
    xmlBuild.root(xmlBuilder(json));
    outBytes = xmlWriter.toString().getBytes("UTF-8");
    }
    ByteArrayInputStream out = new ByteArrayInputStream(outBytes);
    dataContext.storeStream (out , props);
    }

 

def String streamToString(InputStream input)
{
    String outString = '''''';
    input.eachLine { line ->
        outString = outString + line + '\n';
    }
    return outString;
}

 

def xmlBuilder(map) {
return {
    map.each { key , value ->
    if (key == null || key == "")
    {
        key = "element";
    }
    if (value instanceof Map) {
        "$key" xmlBuilder(value);
    }
    else if (value instanceof List) {
        "$key"{
        for (i=0;i < value.size; i++)
        {
        def arr = value.get(i);
        "element"("$arr");
        }
        }
    }
    else {
        "$key"("$value");
    }
    }
}
}

 

def jsonBuilder (node) {
   if (node instanceof String){
        return
    }
    def jsonMap = [:]
    if (!node.attributes().isEmpty()) {
        jsonMap.put('attributes', node.attributes().collectEntries{it})
    }
    if (!node.children().isEmpty() && !(node.children().get(0) instanceof String)) {
        jsonMap.put(node.name() , node.children().collect{jsonBuilder(it)}.findAll{it != null})
    } else if (node.text() != ''){
        jsonMap.put(node.name() , node.text())
}
return jsonMap
}

This runs perfectly fine in my system.
But boomi has this error

Error:

Unable to determine the current character, it is not a string, number, array, or object The current character read is '<' with an int value of 60 Unable to determine the current character, it is not a string, number, array, or object

Outcomes