Skip to content

Instantly share code, notes, and snippets.

@rskuipers
Last active July 30, 2018 18:05
Show Gist options
  • Save rskuipers/9210102ae01dfe17d833007ae5aa32b7 to your computer and use it in GitHub Desktop.
Save rskuipers/9210102ae01dfe17d833007ae5aa32b7 to your computer and use it in GitHub Desktop.
Custom extractor for PhpStorm to extract database tables as JSON
function eachWithIdx(iterable, f) { var i = iterable.iterator(); var idx = 0; while (i.hasNext()) f(i.next(), idx++); }
function mapEach(iterable, f) { var vs = []; eachWithIdx(iterable, function (i) { vs.push(f(i));}); return vs; }
function output() { for (var i = 0; i < arguments.length; i++) { OUT.append(arguments[i]); } }
var rows = [];
eachWithIdx(ROWS, function (row) {
var obj = {};
eachWithIdx(COLUMNS, function (col) {
var value = FORMATTER.format(row, col);
if (value.toLowerCase() === "null") {
value = null;
}
try {
value = JSON.parse(value);
} catch (e) { }
obj[col.name()] = value;
});
rows.push(obj);
});
output(JSON.stringify(rows, null, 4));
@rskuipers
Copy link
Author

Open a database table in PhpStorm, in the dropdown of formats to export as click on "Go to Scripts directory".
In "Extensions -> Database Tools and SQL -> data -> extractors" you can create a file called JSON-JavaScript.json.js and paste the above code in. Now you can extract the data as JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment