Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
RuudFl committed Nov 21, 2024
1 parent 071a82c commit b1f2d96
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
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 added build/JsonFlattener.mpk
Binary file not shown.
Binary file added docs/JsonFlattener.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions src/jsonflattener/actions/FlattenJsonToCsv.java
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
}

0 comments on commit b1f2d96

Please sign in to comment.