Last active
November 3, 2021 14:43
-
-
Save markwhitaker/d14d1a38cc69012748080629fbeacf51 to your computer and use it in GitHub Desktop.
DataGrip extractor to extract minified JSON with null values omitted
This file contains 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
/* | |
* Available context bindings: | |
* COLUMNS List<DataColumn> | |
* ROWS Iterable<DataRow> | |
* OUT { append() } | |
* FORMATTER { format(row, col); formatValue(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 Tuple: | |
printJSON(level, o[0], o[1]) | |
break | |
case Map: | |
OUT.append("{") | |
o.findAll { k, v -> v != null && v[1] && v[1] != null }.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("[") | |
def plain = true | |
o.eachWithIndex { item, i -> | |
plain = item == null || item instanceof Number || item instanceof Boolean || item instanceof String | |
if (plain) { | |
OUT.append(i > 0 ? "," : "") | |
} | |
else { | |
OUT.append("${i > 0 ? "," : ""}") | |
} | |
printJSON(level + 1, col, item) | |
} | |
OUT.append("]") | |
break | |
default: | |
if (DIALECT.getDbms().isMongo()) { | |
withType(o, col, { printPrimitiveValue(o, col) }) | |
} | |
else { | |
printPrimitiveValue(o, col) | |
} | |
break | |
} | |
} | |
def printPrimitiveValue(o, col) { | |
switch (o) { | |
case null: OUT.append("null"); break | |
case Double.NaN: | |
case Double.NEGATIVE_INFINITY: | |
case Double.POSITIVE_INFINITY: | |
OUT.append("\"$o\"") | |
break | |
case Number: | |
OUT.append(FORMATTER.formatValue(o, col)) | |
break | |
case Boolean: OUT.append("$o"); break | |
default: | |
def str = o instanceof String ? o : FORMATTER.formatValue(o, col) | |
OUT.append("\"${escapeStr(str)}\""); break | |
break | |
} | |
} | |
def withType(o, col, func) { | |
def typeName = FORMATTER.getTypeName(o, col) | |
if (typeName == "timestamp" || typeName == "regex" || typeName == "binData") { | |
def jsonTypeName = typeName == "timestamp" ? "timestamp" : | |
typeName == "regex" ? "regularExpression" : | |
"binary" | |
OUT.append("{\"\$$jsonTypeName\": ") | |
OUT.append(FORMATTER.formatValue(o, col)) | |
OUT.append("}") | |
return | |
} | |
def jsonTypeName = typeName == "objectId" ? "oid" : | |
typeName == "date" ? "date" : | |
typeName == "decimal" ? "numberDecimal" : | |
typeName == "minKey" ? "minKey" : | |
typeName == "maxKey" ? "maxKey" : | |
o == Double.NaN || o == Double.POSITIVE_INFINITY || o == Double.NEGATIVE_INFINITY ? "numberDouble" : | |
null | |
if (jsonTypeName != null) OUT.append("{\"\$$jsonTypeName\": ") | |
func() | |
if (jsonTypeName != null) OUT.append("}") | |
} | |
printJSON(0, null, ROWS.transform { row -> | |
def map = new LinkedHashMap<String, String>() | |
COLUMNS.each { col -> | |
if (row.hasValue(col)) { | |
def val = row.value(col) | |
map.put(col.name(), new Tuple(col, val)) | |
} | |
} | |
map | |
}) |
This file contains 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
MIT License | |
Copyright (c) 2020 Mark Whitaker | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment