CSV to JSON Converter Script
Write a Perl script that converts CSV files into JSON. It reads a CSV with a header row and outputs a JSON array of objects, one per row, using the header names as keys. For example, a row with name, age and active becomes {"name":"Ana","age":31,"active":true}, with numbers and true/false values turned into real JSON numbers and booleans instead of text. It handles tricky CSV correctly, such as commas and quotes inside fields, and reports broken rows clearly. It's a practical first Perl project: you'll learn to use CPAN modules (Text::CSV and JSON::PP), work with arrays and hashes, write simple regular expressions, and build a tidy command-line tool.
- Estimate
- ~7.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
Text::CSV · JSON::PP
Project roadmap
- 01
Set up the script
~1hCreate csv2json.pl with use strict and use warnings, and install Text::CSV (JSON::PP comes with Perl). Use Getopt::Long for options: an input file (or - for standard input), -o for an output file (default: standard output), --pretty and --help. Add samples/people.csv with a header and about 10 rows, including a field with a comma inside quotes. Done when: the script prints usage with --help and fails clearly for a missing file.
- 02
Read the CSV with Text::CSV
~1.5hCreate a Text::CSV object with binary => 1 and auto_diag => 1, and read the header row with the header method. Then read each row with getline_hr, which returns a hash reference with the header names as keys. Handle quoted fields with commas and line breaks correctly. Done when: the script reads every row of the sample file, including the quoted field.
- 03
Convert values to the right types
~1.5hWrite convert_value($text) that turns whole numbers and decimals (checked with a regular expression, so 007 and 1e5 are handled on purpose) into numbers, true and false (any upper or lower case) into JSON::PP::true and JSON::PP::false, empty strings into null (undef), and leaves everything else as text. Done when: the output has real numbers, booleans and nulls where they belong.
- 04
Write the JSON
~1hCollect the converted rows in an array and encode it with JSON::PP, using canonical => 1 so keys always appear in the same order, and pretty => 1 when --pretty is given. Encode the text as UTF-8 so accented characters survive. Write to the output file or standard output. Done when: the output is valid JSON (check it with a validator such as jq) and matches the input.
- 05
Handle broken input
~1hReport rows with more or fewer fields than the header as warnings with their line number, and skip them, or stop with an error when --strict is given. Also handle an empty file, a file with only a header, and duplicate column names. Print a summary to standard error with the number of rows converted and skipped. Done when: a CSV with a bad row reports it with its line number and still converts the rest.
- 06
Write tests
~1.5hWrite tests in t/ with Test::More for convert_value (numbers, decimals, booleans, empty values, text that looks like a number) and for converting small CSV strings, including quoted commas and a bad row, to the expected data. Done when: prove -l t/ passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack