Question
2
Replies
1764
Views
Posted: April 16, 2018
Last activity: August 2, 2018
Closed
Create a Json file
Usually, I use a Connect-File rule to create files and source a property that has JSON string. But, if I want to the name of this file dynamically set depending on the pxInsName of a data instance, how do I achieve it ? Using a Connect-File rule is not mandatory.
Hi,
You can try using the following java step method.
Java Step for Saving JSON data as a file
//Getting the Step Page fo Getting Actual JSON Data
ClipboardPage StepPage = tools.getStepPage();
String JsonResponse = StepPage.getString("TzJsonRequest");
oLog.infoForced(JsonResponse);
String ServiceExportDir = pega.getStaticContentUtils().getServiceExportRoot();
String FilePath = ServiceExportDir+"UVUOutPut"+java.io.File.separator+pxInsName +".txt";
oLog.infoForced("Actual File Path of JSON File "+FilePath);
tools.getParameterPage().putString("JSONFilePath",FilePath);
try{
//Creating a PR File Object
com.pega.pegarules.pub.util.PRFile ServiceExportFile = new com.pega.pegarules.pub.util.PRFile(FilePath);
com.pega.pegarules.pub.util.PROutputStream ServiceExportFileOut = new com.pega.pegarules.pub.util.PROutputStream(ServiceExportFile);
//ServiceExportFileOut.write(org.apache.commons.codec.binary.Base64.decodeBase64(JsonResponse));
byte[] contentInBytes = JsonResponse.getBytes();
ServiceExportFileOut.write(contentInBytes);
ServiceExportFileOut.close();
}
catch(java.io.IOException ie)
{
ie.printStackTrace();
oLog.infoForced("Error writing the file content to Service Export Directory", ie);
}