SHARE
TWEET

Untitled

a guest Jul 17th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs")
  2.  
  3. const rawSourceData = fs.readFileSync("./storage/file1.json")
  4. const sourceData = JSON.parse(rawSourceData)
  5.  
  6. function exportData(data, result) {
  7.     console.log(data)
  8.     if (Array.isArray(data)) {
  9.         data.forEach(it => {
  10.             if (it != null) exportData(it, result)
  11.         })
  12.     } else if (typeof data === "object") {
  13.         if (data != null) Object.keys(data).forEach(it => {
  14.             if (data[it] != null) exportData(data[it], result)
  15.         })
  16.     } else if (typeof data === "string") {
  17.         result.push(data)
  18.     }
  19.     return result
  20. }
  21.  
  22. function importData(data, array = []) {
  23.     if (Array.isArray(data)) {
  24.         data.forEach((it, index) => {
  25.             if (it != null) data[index] = importData(it, array)
  26.         })
  27.     } else if (typeof data === "object") {
  28.         console.log(Object.keys(data)) // <---
  29.         if (data != null) Object.keys(data).forEach(key => { if (data[key] != null) data[key] = importData(data[key], array) })
  30.     } else if (typeof data === "string") {
  31.         return array.shift()
  32.     } else {
  33.         return data
  34.     }
  35.     return data
  36. }
  37.  
  38. function processExport() {
  39.     const exported = exportData(sourceData, [])
  40.     // console.log("exported", exported)
  41.     fs.writeFileSync("./results/exported.json", JSON.stringify(exported))
  42. }
  43.  
  44. function processImport() {
  45.     const rawExported = fs.readFileSync("./results/exported.json")
  46.     const exported = JSON.parse(rawExported)
  47.     const imported = importData(sourceData, exported)
  48.     // console.log("imported", imported)
  49.     fs.writeFileSync("./results/processed.json", JSON.stringify(imported))
  50. }
  51.  
  52. processExport()
  53. // processImport()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top