Script that re-writes a JSON export from phpMyAdmin so that numeric fields are not strings. For example, it takes the JSON export of:
[{ "type":"table","name":"myuser","database":"mysql", "data": [
{"id": "3","firstName":"john","password":null,"maybeAFloat":"86.2"},
{"id": "4","firstName":"mary","password":"*FF80A76950918AA85","maybeAFloat":"192.168.1.1"}]
}]
yields the output of (with some whitespace removed):
[{"type": "table", "name": "myuser", "data":
[{"id": 3, "firstName": "john", "password": null, "maybeAFloat": 86.2, },
{"id": 4, "firstName": "mary", "password": "*FF80A76950918AA85", "maybeAFloat": "192.168.1.1",}]
}]
Notice the values of 3
, 4
, 86.2
and null
are not strings, yet 192.168.1.1
remains a string. The main benefit here is that the numeric fields are output as Javascript numerics and not strings. If used to import to something like Firestore, the values will maintain their Javascript datatype, not simply all being stored as strings.
- Clone this repository to your machine. For example:
git clone https://github.com/gregfenton/convert-phpmyadmin-json-export
- Go to the demo phpMyAdmin site, log in as user: root pass: (no password)
- At the top of the window, click Export
- Set:
- Export method: quick
- Format: JSON
- Click Go
- You just downloaded a JSON file. Copy it to the same directory that
convertPhpJsonExport.js
is in. - Rename the JSON file so that its extension is
.js
. For example:mv 192_168_30_23.json 192_168_30_23.js
- Edit the JSON file by adding the following as the first line of the file:
-
exports.mysqlData =
-
- In the file convertPhpJsonExport.js, for the line that sets the
jsonData
variable, load the downloaded JSON data. For example:-
var jsonData = require("./192_168_30_23").mysqlData;
-
- Run the code in this script:
node convertPhpJsonExport.js > convertedOutput.json
By default, debug logging is turned off. You can enable it, simply modify the line:
const doLog = false;
to
const doLog = true;
Follow steps 7 thru 10 using your own export .json file.