Skip to content

Instantly share code, notes, and snippets.

@txomon
Last active April 8, 2025 20:58
Show Gist options
  • Save txomon/99f35e27467c5905343e184c11308843 to your computer and use it in GitHub Desktop.
Save txomon/99f35e27467c5905343e184c11308843 to your computer and use it in GitHub Desktop.
Jetbrains IDEs (Pycharm, Intellij, etc.) data export to JSONL (json lines)
/*
* 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 printJSON(level, col, o) {
switch (o) {
case null: OUT.append("null"); break
case Tuple: printJSON(level, o[0], o[1]); break
case Map:
OUT.append("{")
o.entrySet().eachWithIndex { entry, i ->
OUT.append("${i > 0 ? "," : ""}")
OUT.append("\"${escapeStr(entry.getKey().toString())}\"")
OUT.append(":")
printJSON(level + 1, col, entry.getValue())
}
OUT.append("}")
break
case Object[]:
case Iterable:
OUT.append("[")
o.eachWithIndex { item, i ->
OUT.append(i > 0 ? "," : "")
printJSON(level + 1, 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
}
}
ROWS.each { row ->
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))
}
}
printJSON(0, null, map)
OUT.append("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment