Last active
December 4, 2025 18:09
-
-
Save ksonbol/23fe04e6f4ab9bab3dbc3a63e4a49423 to your computer and use it in GitHub Desktop.
IntelliJ IDEA extractor script for SQL output for JSON format but with each row on a new line
This file contains hidden or 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
| /* | |
| * NOTE: This script was generated with the help of Claude AI | |
| * To make it accessible, download the file and add it in the this directory inside | |
| * your IntelliJ installation: | |
| * "extensions/com.intellij.database/data/extractors/JSON-Flat.json.groovy" | |
| * Your intelliJ installation on mac can be found at: "/Users/your.user/Library/Application Support/JetBrains/IntelliJIdea{VERSION}" | |
| * | |
| * Available context bindings: | |
| * COLUMNS List<DataColumn> | |
| * ROWS Iterable<DataRow> | |
| * OUT { append() } | |
| * FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); } | |
| * TRANSPOSED Boolean | |
| * plus ALL_COLUMNS, TABLE, DIALECT | |
| * | |
| * where: | |
| * DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object } | |
| * DataColumn { columnNumber(), name() } | |
| */ | |
| import static com.intellij.openapi.util.text.StringUtil.escapeStringCharacters as escapeStr | |
| def printFlatJSON(col, o) { | |
| switch (o) { | |
| case null: OUT.append("null"); break | |
| case Tuple: printFlatJSON(o[0], o[1]); break | |
| case Map: | |
| OUT.append("{") | |
| o.entrySet().eachWithIndex { entry, i -> | |
| if (i > 0) OUT.append(", ") | |
| OUT.append("\"${escapeStr(entry.getKey().toString())}\": ") | |
| printFlatJSON(col, entry.getValue()) | |
| } | |
| OUT.append("}") | |
| break | |
| case Object[]: | |
| case Iterable: | |
| OUT.append("[") | |
| o.eachWithIndex { item, i -> | |
| if (i > 0) OUT.append(", ") | |
| printFlatJSON(col, item) | |
| } | |
| OUT.append("]") | |
| break | |
| case Boolean: OUT.append("$o"); break | |
| default: | |
| def str = FORMATTER.formatValue(o, col) | |
| def typeName = FORMATTER.getTypeName(o, col) | |
| def shouldQuote = FORMATTER.isStringLiteral(o, col) && !(typeName.equalsIgnoreCase("json") || typeName.equalsIgnoreCase("jsonb")) | |
| OUT.append(shouldQuote ? "\"${escapeStr(str)}\"" : str); | |
| break | |
| } | |
| } | |
| OUT.append("[") | |
| OUT.append(System.getProperty("line.separator")) | |
| def rowsList = ROWS.toList() | |
| rowsList.eachWithIndex { row, index -> | |
| def map = new LinkedHashMap<String, Object>() | |
| COLUMNS.each { col -> | |
| if (row.hasValue(col)) { | |
| def val = row.value(col) | |
| map.put(col.name(), new Tuple(col, val)) | |
| } | |
| } | |
| OUT.append(" ") | |
| printFlatJSON(null, map) | |
| if (index < rowsList.size() - 1) { | |
| OUT.append(",") | |
| } | |
| OUT.append(System.getProperty("line.separator")) | |
| } | |
| OUT.append("]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment