AnsweredAssumed Answered

How to split JSON string values and map it to a JSON Array elements

Question asked by amudhavani.shanthosh181426 on May 23, 2018
Latest reply on May 24, 2018 by mallari.saikumargoud353789

Hi,

 

The requirement is as follows :

Both Source and Destination profile are in JSON format.

The JSON source profile has a string field with value ""Xray;A P CAP 250MG"

It needs to be mapped to the destinaton JSON profile element which is of type Array (Repeating) in the following format

["Xray","A P CAP 250MG"]

 

I tried the below script in the Data Process Shape and the output of the data process shape is as follows :

Xray,A P CAP 250MG

 

I stored the above output in a set property shape to a DPP as "current data". And mapped this DPP in the map shape to the destination profile Array element and the output is as follows :

 

"SecD11" : [
"Xray,A P CAP 250MG"
],

^^ which is an incorrect Array format. 

 

Can you please help me in splitting the JSON string elements and adding it to the JSON Array elements. Thanks.

 

Code done in the data process shape :

----------------------------------------------------

import java.io.InputStream;
import java.io.BufferedReader;
import java.util.Properties;
import com.boomi.execution.ExecutionUtil;
import java.text.*;

logger = ExecutionUtil.getBaseLogger();

String LINE_SEPARATOR = System.getProperty("line.separator");

for( int i = 0; i < dataContext.getDataCount(); i++ ) {

InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer outData = new StringBuffer();
int lineNum = 0;

while ((line = reader.readLine()) != null) {

String[] eachString = line.split(";");

int numberOfDates = eachString.length;
logger.info("numberOfDates: " + numberOfDates);

String res = "";
for(int j=0;j<numberOfDates;j++){

res=res+eachString[j];
if(j<eachString.length-1){
res=res+",";
}
}

outData.append(res);
}
// Convert the output StringBuffer to an InputStream and store in the dataContext
is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));
dataContext.storeStream(is, props);

}

Outcomes