-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# JsonFlattener | ||
Flatten JSON to CSV. Based on https://github.com/opendevl/Json2Flat | ||
|
||
----------------------------------- | ||
INSTRUCTIONS: | ||
|
||
1) Create a filedocument (or a specialization of a filedocument) in a microflow | ||
2) Add the java action to your microflow and add a JSON string and the created filedocument as input parameters | ||
|
||
Dependencies: | ||
|
||
- Mendix Runtime 9.24.29 or higher |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// This file was generated by Mendix Studio Pro. | ||
// | ||
// WARNING: Only the following code will be retained when actions are regenerated: | ||
// - the import list | ||
// - the code between BEGIN USER CODE and END USER CODE | ||
// - the code between BEGIN EXTRA CODE and END EXTRA CODE | ||
// Other code you write will be lost the next time you deploy the project. | ||
// Special characters, e.g., é, ö, à, etc. are supported in comments. | ||
|
||
package jsonflattener.actions; | ||
|
||
import com.mendix.core.Core; | ||
import com.mendix.systemwideinterfaces.core.IContext; | ||
import com.mendix.systemwideinterfaces.core.IMendixObject; | ||
import com.mendix.webui.CustomJavaAction; | ||
import java.io.ByteArrayInputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import com.github.opendevl.JFlat; | ||
import com.mendix.logging.ILogNode; | ||
|
||
public class FlattenJsonToCsv extends CustomJavaAction<java.lang.Boolean> | ||
{ | ||
private java.lang.String jsonString; | ||
private IMendixObject __csvOutput; | ||
private system.proxies.FileDocument csvOutput; | ||
|
||
public FlattenJsonToCsv(IContext context, java.lang.String jsonString, IMendixObject csvOutput) | ||
{ | ||
super(context); | ||
this.jsonString = jsonString; | ||
this.__csvOutput = csvOutput; | ||
} | ||
|
||
@java.lang.Override | ||
public java.lang.Boolean executeAction() throws Exception | ||
{ | ||
this.csvOutput = this.__csvOutput == null ? null : system.proxies.FileDocument.initialize(getContext(), __csvOutput); | ||
|
||
// BEGIN USER CODE | ||
|
||
try { | ||
// Flatten JSON | ||
JFlat flatMe = new JFlat(jsonString); | ||
flatMe.json2Sheet().headerSeparator("_").getJsonAsSheet(); | ||
|
||
// Output CSV | ||
String csvFilePath = "temp.csv"; | ||
flatMe.write2csv(csvFilePath); | ||
|
||
// Put contents in file | ||
byte[] csvBytes = Files.readAllBytes(Paths.get(csvFilePath)); | ||
ByteArrayInputStream csvInputStream = new ByteArrayInputStream(csvBytes); | ||
Core.storeFileDocumentContent(getContext(), this.csvOutput.getMendixObject(), csvInputStream); | ||
|
||
// Delete temp file | ||
Path tempFilePath = Paths.get(csvFilePath); | ||
Files.delete(tempFilePath); | ||
|
||
return true; | ||
|
||
} catch (Exception e) { | ||
LOGGER.warn("Failed to convert CSV file: " + e.getMessage(), e); | ||
return false; // Operation failed | ||
} | ||
|
||
// END USER CODE | ||
} | ||
|
||
/** | ||
* Returns a string representation of this action | ||
* @return a string representation of this action | ||
*/ | ||
@java.lang.Override | ||
public java.lang.String toString() | ||
{ | ||
return "FlattenJsonToCsv"; | ||
} | ||
|
||
// BEGIN EXTRA CODE | ||
private static final ILogNode LOGGER = Core.getLogger("JsonFlattener"); | ||
// END EXTRA CODE | ||
} |