Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

multiple-csv-merge-to-json

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multiple-csv-merge-to-json

Merge multiple CSV files and output to JSON file

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

multiple-csv-merge-to-json

This library reads a given list of CSV files and stores the compilation of data in a JSON file. The files are merged following a key, which can be one or several columns. Files should be given in order of retention data. If key was already in previous file, data is merged and replaced by lastest data only once. If key is found several times, a new line is added, merged with previous data.

Why ?

I had a scenario with 3 spreadsheets with different columns to merge. Instead of making crazy spreadsheet formulas, I decided to code a JS module.

Using module csvtojson for single CSV file read and convertion to JSON.

How to use

options API MultCsvMergeToJsonOptions

optiondescriptionmandatorytype
inputDirDirectory where the csv files aretruestring
inputKeysUnique column keys found in every CSV filetrueArray
inputFileNameListList of filenames, in import ordertrueArray
outputDirDestination folder for generated JSON filetruestring
outputFileNameFile name of generated JSON filetruestring
columnDelimiterCSV column separatortruestring
encodingCSV files encoding, default to 'utf8'nostring
groupByGroup data by keyno{ groupByKey: string; groupedArrayProperty: string }
writeToFileWrite to file, default to falsenoboolean
replaceValuesReplace values from one file to another, default to falsenoboolean

mergeCsvFilesToJsonArray(options: MultCsvMergeToJsonOptions)

Function reads every file given in option and merges data in a JSON containing an array of objects. JSON object is saved in a file .json format.

  • params : MultCsvMergeToJsonOptions
  • returns : void, executes file reading and writing
mergeCsvFilesToJsonArray({
  inputDir: "./data_input_files",
  inputKeys: ["city", "region"],
  inputFileNameList: [
    "general_rates.csv",
    "premium_rates.csv",
    "danger_zones.csv",
  ],
  outputDir: "./data_output_json",
  outputFileName: "delivery_rates",
  columnDelimiter: ",",
});

getJsonArray(options: MultCsvMergeToJsonOptions)

Function finds and reads JSON file given in options, and returns its object in Javascript. File is meant to be previously generated by mergeCsvFilesToJsonArray function.

  • params : MultCsvMergeToJsonOptions
  • returns : Array array containing objects found in JSON file.
    const objectsList = await getJsonArray({
      inputDir: "./data_input_files",
      inputKeys: ["city", "region"],
      inputFileNameList: [
        "general_rates.csv",
        "premium_rates.csv",
        "danger_zones.csv",
      ],
      outputDir: "./data_output_json",
      outputFileName: "delivery_rates",
      columnDelimiter: ",",
    });
    

    Example

    Given three CSV files as input

    File 1

    cityregiondeliveryScheduleratedeliveryInstruction
    AHUACHAPANAHUACHAPANLUNES-MIERCOLES-VIERNES3Contacto por telefono
    APOPASAN SALVADORDE LUNES A SABADO3
    AYUTUXTEPEQUESAN SALVADORDE LUNES A SABADO3
    MEJICANOSSAN SALVADORDE LUNES A SABADO3
    SAN SALVADORSAN SALVADORDE LUNES A SABADO3

    File 2

    cityregiondeliverySchedulerate
    ApopaSan SalvadorLunes a Sabado4
    AyutuxtepequeSan SalvadorLunes a Sabado4
    San SalvadorSan SalvadorLunes a Sabado4

    File 3

    localitycityregionriskdeliveryInstruction
    Madre SelvaApopaSan SalvadorDelincuenciaPUNTO DE ENCUENTRO
    PopotlanApopaSan SalvadorDelincuenciaPUNTO DE ENCUENTRO
    EL TIGREAHUACHAPANAHUACHAPANDelincuenciaPUNTO DE ENCUENTRO
    CTON EL ROSARIOAHUACHAPANAHUACHAPANDelincuenciaPUNTO DE ENCUENTRO

    Expected MERGED data :

    cityregionlocalityriskdeliveryInstructiondeliverySchedulerate
    ApopaSan SalvadorMadre SelvaDelincuenciaPUNTO DE ENCUENTRODE LUNES A SABADO4
    ApopaSan SalvadorPopotlanDelincuenciaPUNTO DE ENCUENTRODE LUNES A SABADO4
    AHUACHAPANAHUACHAPANEL TIGREDelincuenciaPUNTO DE ENCUENTROLUNES-MIERCOLES-VIERNES3
    AHUACHAPANAHUACHAPANCTON EL ROSARIODelincuenciaPUNTO DE ENCUENTROLUNES-MIERCOLES-VIERNES3
    AyutuxtepequeSan SalvadorLunes a Sabado4
    San SalvadorSan SalvadorLunes a Sabado4
    MEJICANOSSAN SALVADORDE LUNES A SABADO3

    Output json

    Execute :

    mergeCsvFilesToJsonArray({
      inputDir: "./data_input_files",
      inputKeys: ["city", "region"],
      inputFileNameList: [
        "general_rates.csv",
        "premium_rates.csv",
        "danger_zones.csv",
      ],
      outputDir: "./data_output_json",
      outputFileName: "delivery_rates",
      columnDelimiter: ",",
    });
    

    Result in file saved :

    [
      {
        "city": "AHUACHAPAN",
        "region": "AHUACHAPAN",
        "deliverySchedule": "LUNES-MIERCOLES-VIERNES",
        "rate": "3",
        "deliveryInstruction": "PUNTO DE ENCUENTRO",
        "locality": "EL TIGRE",
        "risk": "DELINCUENCIA"
      },
      {
        "city": "APOPA",
        "region": "SAN SALVADOR",
        "deliverySchedule": "LUNES A SABADO",
        "rate": "4",
        "deliveryInstruction": "PUNTO DE ENCUENTRO",
        "locality": "MADRE SELVA",
        "risk": "DELINCUENCIA"
      },
      {
        "city": "AYUTUXTEPEQUE",
        "region": "SAN SALVADOR",
        "deliverySchedule": "LUNES A SABADO",
        "rate": "4",
        "deliveryInstruction": ""
      },
      {
        "city": "MEJICANOS",
        "region": "SAN SALVADOR",
        "deliverySchedule": "DE LUNES A SABADO",
        "rate": "3",
        "deliveryInstruction": ""
      },
      {
        "city": "SAN SALVADOR",
        "region": "SAN SALVADOR",
        "deliverySchedule": "LUNES A SABADO",
        "rate": "4",
        "deliveryInstruction": ""
      },
      {
        "city": "APOPA",
        "region": "SAN SALVADOR",
        "deliverySchedule": "LUNES A SABADO",
        "rate": "4",
        "deliveryInstruction": "PUNTO DE ENCUENTRO",
        "updated": true,
        "locality": "POPOTLAN",
        "risk": "DELINCUENCIA"
      },
      {
        "city": "AHUACHAPAN",
        "region": "AHUACHAPAN",
        "deliverySchedule": "LUNES-MIERCOLES-VIERNES",
        "rate": "3",
        "deliveryInstruction": "PUNTO DE ENCUENTRO",
        "updated": true,
        "locality": "CTON EL ROSARIO",
        "risk": "DELINCUENCIA"
      }
    ]
    

    Output json with group by

    Execute :

    mergeCsvFilesToJsonArray({
      inputDir: "./data_input_files",
      inputKeys: ["city", "region"],
      inputFileNameList: [
        "general_rates.csv",
        "premium_rates.csv",
        "danger_zones.csv",
      ],
      outputDir: "./data_output_json",
      outputFileName: "delivery_rates",
      columnDelimiter: ",",
      groupBy: { groupByKey: "region", groupedArrayProperty: "cities" },
    });
    

    Result in file saved :

    [
      {
        "region": "AHUACHAPAN",
        "cities": [
          {
            "city": "AHUACHAPAN",
            "region": "AHUACHAPAN",
            "deliverySchedule": "LUNES-MIERCOLES-VIERNES",
            "rate": "3",
            "deliveryInstruction": "PUNTO DE ENCUENTRO",
            "locality": "EL TIGRE",
            "risk": "DELINCUENCIA"
          },
          {
            "city": "AHUACHAPAN",
            "region": "AHUACHAPAN",
            "deliverySchedule": "LUNES-MIERCOLES-VIERNES",
            "rate": "3",
            "deliveryInstruction": "PUNTO DE ENCUENTRO",
            "updated": true,
            "locality": "CTON EL ROSARIO",
            "risk": "DELINCUENCIA"
          }
        ]
      },
      {
        "region": "SAN SALVADOR",
        "cities": [
          {
            "city": "APOPA",
            "region": "SAN SALVADOR",
            "deliverySchedule": "LUNES A SABADO",
            "rate": "4",
            "deliveryInstruction": "PUNTO DE ENCUENTRO",
            "locality": "MADRE SELVA",
            "risk": "DELINCUENCIA"
          },
          {
            "city": "AYUTUXTEPEQUE",
            "region": "SAN SALVADOR",
            "deliverySchedule": "LUNES A SABADO",
            "rate": "4",
            "deliveryInstruction": ""
          },
          {
            "city": "MEJICANOS",
            "region": "SAN SALVADOR",
            "deliverySchedule": "DE LUNES A SABADO",
            "rate": "3",
            "deliveryInstruction": ""
          },
          {
            "city": "SAN SALVADOR",
            "region": "SAN SALVADOR",
            "deliverySchedule": "LUNES A SABADO",
            "rate": "4",
            "deliveryInstruction": ""
          },
          {
            "city": "APOPA",
            "region": "SAN SALVADOR",
            "deliverySchedule": "LUNES A SABADO",
            "rate": "4",
            "deliveryInstruction": "PUNTO DE ENCUENTRO",
            "updated": true,
            "locality": "POPOTLAN",
            "risk": "DELINCUENCIA"
          }
        ]
      }
    ]
    

Keywords

FAQs

Package last updated on 25 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc