Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sql to json , sqltojson DatabaseResultToParser.java
package org.jrichardsz.horus.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class DatabaseResultToParser {
public Object convertBiDimensionalDataToHeaderDetail(List<Map<String, Object>> databaseResult,
String headerColumns, String detailColumns, String detailAlias) {
if (databaseResult == null || databaseResult.size() == 0) {
throw new IllegalArgumentException("databaseResult is wrong or empty");
}
List<String> expectedHeaderColumns = Arrays.asList(headerColumns.replace("\\s", "").split(","));
// assumption 1: header columns are the same for all rows
LinkedHashMap<String, Object> parsedData =
getExpectedColumns(expectedHeaderColumns, databaseResult.get(0));
List<String> expectedDetailColumns = Arrays.asList(detailColumns.replace("\\s", "").split(","));
ArrayList<Map<String, Object>> detailData = new ArrayList<Map<String, Object>>();
for (Map<String, Object> row : databaseResult) {
Map<String, Object> detail = getExpectedColumns(expectedDetailColumns, row);
detailData.add(detail);
}
// create the expected object
parsedData.put(detailAlias, detailData);
return parsedData;
}
private LinkedHashMap<String, Object> getExpectedColumns(List<String> expectedColumns,
Map<String, Object> row) {
LinkedHashMap<String, Object> headerData = new LinkedHashMap<String, Object>();
for (Entry<String, Object> entry : row.entrySet()) {
if (expectedColumns.contains(entry.getKey())) {
headerData.put(entry.getKey(), entry.getValue());
}
}
return headerData;
}
}
package org.jrichardsz.common.parser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.jrichardsz.horus.common.DatabaseResultToParser;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import junit.framework.TestCase;
import net.minidev.json.JSONArray;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DatabaseResultToParserTest extends TestCase {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Test
public void test_001() throws Exception {
DatabaseResultToParser parser = new DatabaseResultToParser();
Object parsedObject = parser.convertBiDimensionalDataToHeaderDetail(getTestOptions(),
"firstName,position,role", "optionId,optionLabel", "options");
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(parsedObject);
logger.debug(json);
ReadContext ctx = JsonPath.parse(json);
assertEquals("john", (String) ctx.read("$.firstName"));
assertEquals("alumno", (String) ctx.read("$.role"));
JSONArray options = ctx.read("$.options");
assertEquals(3, options.size());
assertEquals("configuracion", (String) ctx.read("$.options[0].optionId"));
assertEquals("Enlaces a otros sistemas", (String) ctx.read("$.options[2].optionLabel"));
}
private List<Map<String, Object>> getTestOptions() {
List<Map<String, Object>> options = new ArrayList<Map<String, Object>>();
HashMap<String, Object> row1 = new HashMap<String, Object>();
row1.put("firstName", "john");
row1.put("position", "alumno");
row1.put("role", "alumno");
row1.put("optionId", "configuracion");
row1.put("optionLabel", "Configuracion");
HashMap<String, Object> row2 = new HashMap<String, Object>();
row2.put("firstName", "john");
row2.put("position", "alumno");
row2.put("role", "alumno");
row2.put("optionId", "calendario-academico");
row2.put("optionLabel", "Calendario Academico");
HashMap<String, Object> row3 = new HashMap<String, Object>();
row3.put("firstName", "john");
row3.put("position", "alumno");
row3.put("role", "alumno");
row3.put("optionId", "enlaces-a-otros-sistemas");
row3.put("optionLabel", "Enlaces a otros sistemas");
options.add(row1);
options.add(row2);
options.add(row3);
return options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment