Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs")
- const rawSourceData = fs.readFileSync("./storage/file1.json")
- const sourceData = JSON.parse(rawSourceData)
- function exportData(data, result) {
- console.log(data)
- if (Array.isArray(data)) {
- data.forEach(it => {
- if (it != null) exportData(it, result)
- })
- } else if (typeof data === "object") {
- if (data != null) Object.keys(data).forEach(it => {
- if (data[it] != null) exportData(data[it], result)
- })
- } else if (typeof data === "string") {
- result.push(data)
- }
- return result
- }
- function importData(data, array = []) {
- if (Array.isArray(data)) {
- data.forEach((it, index) => {
- if (it != null) data[index] = importData(it, array)
- })
- } else if (typeof data === "object") {
- console.log(Object.keys(data)) // <---
- if (data != null) Object.keys(data).forEach(key => { if (data[key] != null) data[key] = importData(data[key], array) })
- } else if (typeof data === "string") {
- return array.shift()
- } else {
- return data
- }
- return data
- }
- function processExport() {
- const exported = exportData(sourceData, [])
- // console.log("exported", exported)
- fs.writeFileSync("./results/exported.json", JSON.stringify(exported))
- }
- function processImport() {
- const rawExported = fs.readFileSync("./results/exported.json")
- const exported = JSON.parse(rawExported)
- const imported = importData(sourceData, exported)
- // console.log("imported", imported)
- fs.writeFileSync("./results/processed.json", JSON.stringify(imported))
- }
- processExport()
- // processImport()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement