Last active
April 13, 2021 12:14
-
-
Save ntedgi/7040f79a1fddb7a82c5eda2bc3eb19fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/* | |
open network tools | |
copy _msearch response | |
save it to file | |
*/ | |
const createCsvWriter = require('csv-writer').createObjectCsvWriter; | |
const fs = require('fs'); | |
const rowJsonPath = "res.json" | |
const resultOutputFilenName = "flatten2.csv" | |
const pullHitsArray = (file) => { | |
const { responses } = file | |
return responses[0]["hits"]["hits"] | |
} | |
const flatten = (data) => { | |
let result = {}; | |
function recurse(cur, prop) { | |
if (Object(cur) !== cur) { | |
result[prop] = cur; | |
} else if (Array.isArray(cur)) { | |
for (let i = 0,var l = cur.length; i < l; i++) | |
recurse(cur[i], prop + "[" + i + "]"); | |
if (l == 0) | |
result[prop] = []; | |
} else { | |
let isEmpty = true; | |
for (let p in cur) { | |
isEmpty = false; | |
recurse(cur[p], prop ? prop + "." + p : p); | |
} | |
if (isEmpty && prop) | |
result[prop] = {}; | |
} | |
} | |
recurse(data, ""); | |
return result; | |
} | |
const replaceAll = (str, find, replace) => str.replace(new RegExp(find, 'g'), replace); | |
const sanitize = (elem) => { | |
const charsToClean = ["\r", "\n"] | |
let result = elem | |
charsToClean.forEach(char => { | |
result = replaceAll(String(result), char, "") | |
}) | |
return result | |
} | |
let rawdata = fs.readFileSync(rowJsonPath); | |
let file = JSON.parse(rawdata); | |
const hits = pullHitsArray(file) | |
const hitsFlats = hits.map(element => { return flatten(element) }); | |
const headers = Object.keys(hitsFlats[0]).map(key => ({ id: key, title: key })) | |
const recordes = hitsFlats.map(element => { | |
const output = {} | |
Object.keys(element).forEach(key => { | |
output[key] = sanitize(element[key]) | |
}) | |
return output | |
}); | |
const csvWriter = createCsvWriter({ | |
path: resultOutputFilenName, | |
header: headers | |
}); | |
csvWriter | |
.writeRecords(recordes) | |
.then(() => console.log('The CSV file was written successfully')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment