Skip to content

Instantly share code, notes, and snippets.

@txomon
Created June 15, 2023 11:33
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(o) {
switch (o) {
case null: OUT.append("null"); break
case Tuple:
// Extract column and value from tuple and pass to printJSON
def tupleCol = o[0]
def tupleVal = o[1]
printJSON(tupleCol, tupleVal)
break
case Map:
OUT.append("{")
o.entrySet().eachWithIndex { entry, i ->
OUT.append("${i > 0 ? "," : ""}")
OUT.append("\"${escapeStr(entry.getKey().toString())}\"")
OUT.append(": ")
printJSON(entry.getValue())
}
OUT.append("}")
break
case Object[]:
case Iterable:
OUT.append("[")
o.eachWithIndex { item, i ->
OUT.append(i > 0 ? ", " : "")
printJSON(item)
}
OUT.append("]")
break
case Boolean: OUT.append("$o"); break
default:
OUT.append("\"${escapeStr(o)}\"");
break
}
}
def last_column = COLUMNS.size - 1
ROWS.each { row ->
OUT.append("{")
COLUMNS.eachWithIndex { col, i ->
OUT.append("\"${escapeStr(col.name())}\"")
OUT.append(": ")
printJSON(row.value(col))
OUT.append("${i < last_column ? ", " : ""}")
}
OUT.append("}\n") // Append newline after each row
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment