Advertisement
gregfenton

Convert phpMyAdmin JSON Export numeric datatypes

Apr 10th, 2020
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // now available at:  https://github.com/gregfenton/convert-phpmyadmin-json-export
  2.  
  3.  
  4.  
  5. // To use:
  6. // 1. Copy all of this code to a new script file called "convertPhpJsonExport.js"
  7. // 2. Go to the demo phpMyAdmin site, log in as user: root pass: (no password)
  8. //      https://demo.phpmyadmin.net/STABLE/?pma_username=root
  9. // 3. At the top of the window, click "Export"
  10. // 4. Set:
  11. //       Export method: quick
  12. //       Format: JSON
  13. // 5. Click "Go"
  14. // 6. You just downloaded a JSON file.  Copy it to the same directory that
  15. //    convertPhpJsonExport.js is in.
  16. // 7. Rename the JSON file so that its extension is ".js".  For example:
  17. //          mv 192_168_30_23.json 192_168_30_23.js
  18. // 8. Edit the JSON file by adding the following as the first line of the file:
  19. //          exports.mysqlData =
  20. // 9. In the line below these instructions, have the
  21. //    jsonData variable load this newly downloaded JSON data.  For example:
  22. //        var jsonData = require("./192_168_30_23").mysqlData;
  23. // 10. Run the code in this script:
  24. //        node convertPhpJsonExport.js > convertedOutput.json
  25. //
  26. var jsonData = require("./mysqlTestData").mysqlData;
  27.  
  28.  
  29. // set to "true" if you want (a lot of) debugging output
  30. const doLog = false;
  31. const l = (str) => (doLog) ? console.log(str) : null;
  32.  
  33. // determines if the given value needs to be quoted or not
  34. const q = (x) => {
  35.   l("x is", x);
  36.  
  37.   if (x === null) {
  38.     l("returning null");
  39.     return "null";
  40.   } else if (isInteger(x)) {
  41.     l("returning parseInt: ", parseInt(x));
  42.     return parseInt(x);
  43.   } else if (typeof x === 'string' && x.match(/\..*\./g)) {
  44.     l("string has multiple dots:", x);
  45.     return quote(x);
  46.   } else if (typeof x === 'string' && x.match(/[^0-9.]/g)) {
  47.     l("string has non-numerics:", x);
  48.     return quote(x);
  49.   } else if (isFloat(x)) {
  50.     l("returning parseFloat: ", parseFloat(x));
  51.     return parseFloat(x);
  52.   } else if (typeof x === "string") {
  53.     l("returning string: ", quote(x));
  54.     return quote(x);
  55.   } else {
  56.     l("returning ERROR:", typeof x);
  57.     return "ERROR - wasn't expecting:" + typeof x;
  58.   }
  59. }
  60.  
  61. // puts double-quotes around the given value
  62. const quote = (x) => '"' + x + '"';
  63.  
  64. const isInteger = (x) => parseInt(x) == x;
  65. const isFloat = (x) => {
  66.     let y = parseFloat(x);
  67.     l("Y IS: ", y, " :: isNaN?", isNaN(y));
  68.     return (!isNaN(y) && typeof y === 'number');
  69. }
  70.  
  71. // keeps track of line indent amount
  72. var indentCount = 0;
  73. const increaseIndent = (incr) => indentCount += incr;
  74.  
  75. // appends given string with indenting (spaces) and appends a newline
  76. const line = (str = "") => "  ".repeat(indentCount) + str + "\n";
  77.  
  78.  
  79.  
  80. // let's go!!
  81. //
  82. let output = line("[");
  83. increaseIndent(1);
  84.  
  85. // jsonData should be a string that begins with an array of objects: [ {}, {}, {}, ... ]
  86. //
  87. jsonData.forEach((item, index) => {
  88.   // only output tables (not header, database, ...)
  89.   if(item["type"] !== "table") { return; }
  90.  
  91.   // onto the objects
  92.   output += line('{');
  93.   increaseIndent(1);
  94.  
  95.   output += line(quote("type") + ": " + q(item["type"]) + ",");
  96.   output += line(quote("name") + ": " + q(item["name"]) + ",");
  97.   output += line(quote("data") + ":");
  98.   output += line("[\n");
  99.   increaseIndent(1);
  100.  
  101.   item["data"].forEach((item2, index2) => {
  102.     output += line("{");
  103.     increaseIndent(1);
  104.  
  105.     // iterate over item2
  106.     let data = item2;
  107.     Object.keys(data).forEach((key) => {
  108.       output += line(quote(key) + ": " + q(data[key]) + ",");
  109.     })
  110.  
  111.     increaseIndent(-1);
  112.     output += line("}");
  113.   })
  114.  
  115.   increaseIndent(-1);
  116.   output += line("]");
  117.  
  118.   increaseIndent(-1);
  119.   output += '}\n'
  120.  
  121. })
  122.  
  123. increaseIndent(-1);
  124. output += line("]");
  125.  
  126. console.log(output);
  127. return;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement