Skip to content

Instantly share code, notes, and snippets.

@jasperkennis
Created November 1, 2021 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasperkennis/33c5859abb064a8439c21a2f53dd26bf to your computer and use it in GitHub Desktop.
Save jasperkennis/33c5859abb064a8439c21a2f53dd26bf to your computer and use it in GitHub Desktop.
Little script to get some stuff Doro needs
/**
* To use:
* - Save this script as `index.js` and put it in a folder
* - In the same folder, put your json file. It MUST be called `dataset.json`.
* - In a terminal go to the folder with the script.
* - Run `node index.js`
* - CSV will be printed with the data you need.
* - If you want to add other data, put it in the array at the beginning of the script.
*/
// Put the fields you need in this array:
const fieldsToGet = [
'id',
'contact_email',
'created_at',
'total_line_items_price',
]
// Some external scripts, ignore this
const path = require('path')
const fs = require('fs')
// Read the file
const rawFile = fs.readFileSync(
path.join(__dirname, 'dataset.json')
)
const jsonContents = JSON.parse(rawFile.toString())
// Print what is going on
console.log(`Going to check ${jsonContents.orders.length} orders.`)
// Get only the data we want per order
const cleanedData = jsonContents.orders.map((order) => {
const cleanedOrderObject = {}
for (let i = 0; i < fieldsToGet.length; i++) {
const fieldName = fieldsToGet[i]
cleanedOrderObject[fieldName] = order[fieldName]
}
return cleanedOrderObject
})
let resultCsv = `${fieldsToGet.join(',')}\n`
// Add objects to string as csv lines
const cleanedDataAsCsvRows = cleanedData.forEach((obj) => {
const fieldsInOrder = []
for (let i = 0; i < fieldsToGet.length; i++) {
const fieldName = fieldsToGet[i];
fieldsInOrder.push(obj[fieldName])
}
resultCsv = `${resultCsv}${fieldsInOrder.join(',')}\n`
})
console.log(resultCsv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment