|
1 |
| -- **utilities/**: |
2 |
| - - Example Project: A **JSON to CSV file converter** script with options to customize the CSV format. |
3 |
| - - Ideal Contributions: Add support for other file formats, include a GUI for easier use, or enhance error handling. |
| 1 | +Utilities Folder |
| 2 | +This folder contains utility scripts that help in performing various tasks. Each utility is designed to be modular and extendable, allowing for easy integration into other projects. |
| 3 | + |
| 4 | +File Structure |
| 5 | +bash |
| 6 | +utilities/ |
| 7 | +│ |
| 8 | +├── jsontocsv.js # A script to convert JSON files into CSV format |
| 9 | +├── input.json # Sample input JSON file for conversion |
| 10 | +└── README.md # Documentation for the utilities |
| 11 | +Utility Scripts |
| 12 | +1. JSON to CSV Converter |
| 13 | +The json-to-csv.js script converts a JSON file into CSV format. It provides options for customizing the output, such as specifying a delimiter and whether to include headers in the CSV file. |
| 14 | + |
| 15 | +Features |
| 16 | +Custom Delimiter: You can choose a delimiter for the CSV file (default is ,). |
| 17 | +Headers: Includes the option to add or remove headers in the CSV output. |
| 18 | +Error Handling: The script checks if the input JSON is an array of objects and handles errors gracefully. |
| 19 | +Usage |
| 20 | +Prerequisites |
| 21 | +Node.js installed on your system. |
| 22 | +Instructions |
| 23 | +Place your input JSON file in the utilities/ folder and name it input.json or modify the script to point to the correct file path. |
| 24 | +Run the script using Node.js. |
| 25 | +bash |
| 26 | + |
| 27 | +node jsontocsv.js |
| 28 | +This will convert the JSON data from input.json into a CSV file (output.csv) in the same folder. |
| 29 | + |
| 30 | +Options |
| 31 | +You can customize the conversion by passing options to the convertJsonToCsv function: |
| 32 | + |
| 33 | +Delimiter: Specify a custom delimiter (default is ,). |
| 34 | +Headers: Choose whether to include headers in the CSV file (default is true). |
| 35 | +Example of modifying options in the script: |
| 36 | + |
| 37 | +javascript |
| 38 | +Copy code |
| 39 | +convertJsonToCsv(inputFile, outputFile, { |
| 40 | + delimiter: ";", // Use semicolon as the delimiter |
| 41 | + includeHeaders: false, // Skip headers in the output |
| 42 | +}); |
| 43 | +Sample JSON Input (input.json) |
| 44 | +json |
| 45 | +Copy code |
| 46 | +[ |
| 47 | + { |
| 48 | + "name": "John Doe", |
| 49 | + "age": 28, |
| 50 | + "email": "john@example.com" |
| 51 | + }, |
| 52 | + { |
| 53 | + "name": "Jane Doe", |
| 54 | + "age": 25, |
| 55 | + "email": "jane@example.com" |
| 56 | + } |
| 57 | +] |
| 58 | +Sample CSV Output (output.csv) |
| 59 | +perl |
| 60 | +Copy code |
| 61 | +"name","age","email" |
| 62 | +"John Doe",28,"john@example.com" |
| 63 | +"Jane Doe",25,"jane@example.com" |
| 64 | +Error Handling |
| 65 | +The script will throw an error if: |
| 66 | + |
| 67 | +The input file is not in the correct format (e.g., the JSON is not an array of objects). |
| 68 | +The input file cannot be read due to permissions or path issues. |
0 commit comments