1

I have 2 CSV files and I want to grab specific data (while ignoring some columns) from the CSVs and turn them into a JSON format with nested arrays and specific key names. I also want to order the JSON by a specific value (orderID).

Here's an example:

CSV file 1:

[ORDERIDENTIFIER, ORDERDTE, STORESTE, STOREADDRESS, STOREZIP, PRODUCTTYPE]

CSV File 2:

[ORDERTYPE, CUSTOMERAGE, CITY]

JSON:

[
    {
        "storeOrders": {
            "orderType": "2",
            "orderID": "22222",
            "orderDetails": {
                "productType": "Standard"
            },
            "collectedOrders": [
                {
                    "orderDate": "2023-09-03",
                    "customerAge": "22"
                }
            ],
            "storeDetails": {
                "storeState": "IA"
            }
        }
    },
    {
        "storeOrders": {
            "orderType": "1",
            "orderID": "444",
            "orderDetails": {
                "productType": "Standard"
            },
            "collectedOrders": [
                {
                    "orderDate": "2023-06-03",
                    "customerAge": "60"
                }
            ],
            "storeDetails": {
                "storeState": "RI"
            }
        }
    }
]

I have tried using the csvtojson but am unsure how to use it for multiple files/changing key names`

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40

1 Answers1

0

You can define a helper function to read and split csv to multidimensional array by newline and delimiter. Then use map to transform your data to the required array of objects structure. Pass custom function to sort to order by id.

const fs = require('fs');
const readCsvFile = (fileName, delimiter) => fs.readFileSync(fileName, 'utf8')
   .trim().split('\n').slice(1).map(row => row.split(delimiter));

const csv1Data = readCsvFile('csv1.csv', ','), csv2Data = readCsvFile('csv2.csv', ',');
const sortByID = (a, b) => parseInt(a.storeOrders.orderID) - parseInt(b.storeOrders.orderID);

const combinedData = csv1Data.map((row, i) => ({
    storeOrders: {
      orderType: csv2Data[i]?.[0],
      orderID: row[0],
      orderDetails: {productType: row[5]},
      collectedOrders: [ {orderDate: row[1], customerAge: csv2Data[i]?.[1]}],
      storeDetails: { storeState: row[2] }
    }
  }));
  

const sortedData = combinedData.sort(sortByID);

fs.writeFileSync('output.json', JSON.stringify(sortedData, null, 2));
protob
  • 3,317
  • 1
  • 8
  • 19