How to Convert OQL String or CSV Data in JSON Format

0
Hi Team,   As given in the below OQL Playground how to convert OQL data in JSON format dynamically.  OQL Playground OQL Thanks & Regards, Manikandan K
asked
2 answers
0

Hi Manikandan,

Create a Java action, pass an oql string parameter, as shown in the image below,

Screenshot 2024-11-21 121627.png

 

and then put the given Java code inside the Java action.

// 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 searchfiltermodified.actions;

import java.util.List;
import java.util.Map;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.connectionbus.data.IDataRow;
import com.mendix.systemwideinterfaces.connectionbus.data.IDataTable;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.thirdparty.org.json.JSONArray;
import com.mendix.thirdparty.org.json.JSONObject;
import com.mendix.webui.CustomJavaAction;

public class JA_Json_Creation extends CustomJavaAction<java.lang.String>
{
	private java.lang.String Query;

	public JA_Json_Creation(IContext context, java.lang.String Query)
	{
		super(context);
		this.Query = Query;
	}

	@java.lang.Override
	public java.lang.String executeAction() throws Exception
	{
		// BEGIN USER CODE
		IContext context = this.getContext();
		IDataTable resultList =Core.retrieveOQLDataTable(context, Query);
		JSONArray jsonArray = new JSONArray();
		
		int columnCount = resultList.getSchema().getColumnCount();
		for (IDataRow row : resultList.getRows()) {
			JSONObject jsonObject = new JSONObject();
			for (int colIndex = 0; colIndex < columnCount; colIndex++) {
				String columnName = resultList.getSchema().getColumnSchema(colIndex).getName();
				Object value = row.getValue(context, colIndex);
				jsonObject.put(columnName, value != null ? value.toString() : JSONObject.NULL);
			}
			jsonArray.put(jsonObject);
		}
		String jsonString = jsonArray.toString();
//		Core.getLogger("JA_Json_Creation").info(jsonString);
//		Core.getLogger(Query).info(resultList);
	
		return jsonString;
		// 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 "JA_Json_Creation";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

 

answered
0

With the OQL module you can execute your OQL query, and then use a export mapping to transform the result into a json documnt.

 

answered