Convert CSV to JSON using BASH

I have been assigned a task to generate random data in JSON format. I do have a big data set ready in CSV (comma separated values) and would love to convert it to JSON just using BASH. You can copy following codes and save it as a executable script file.

#!/bin/bash
# CSV to JSON converter using BASH
# Usage ./csv2json input.csv > output.json
 
input=$1
 
[ -z $1 ] && echo "No CSV input file specified" && exit 1
[ ! -e $input ] && echo "Unable to locate $1" && exit 1
 
read first_line < $input
a=0
headings=`echo $first_line | awk -F, {'print NF'}`
lines=`cat $input | wc -l`
while [ $a -lt $headings ]
do
        head_array[$a]=$(echo $first_line | awk -v x=$(($a + 1)) -F"," '{print $x}')
        a=$(($a+1))
done
 
c=0
echo "{"
while [ $c -lt $lines ]
do
        read each_line
        if [ $c -ne 0 ]; then
                d=0
                echo -n "{"
                while [ $d -lt $headings ]
                do
                        each_element=$(echo $each_line | awk -v y=$(($d + 1)) -F"," '{print $y}')
                        if [ $d -ne $(($headings-1)) ]; then
                                echo -n ${head_array[$d]}":"$each_element","
                        else
                                echo -n ${head_array[$d]}":"$each_element
                        fi
                        d=$(($d+1))
                done
                if [ $c -eq $(($lines-1)) ]; then
                        echo "}"
                else
                        echo "},"
                fi
        fi
        c=$(($c+1))
done < $input
echo "}"

To perform the conversion, run the script with first argument is the CSV file that you want to convert to and redirect the output to an output file. Make sure the CSV file contains field names as the header, similar to example below:

name,modified,social_security
"Farrah Walters","208-72-8449","1386670785"
"Shay Warner","539-53-2690","1386644172"
"Maxine Norton","231-61-5065","1386658663"

Hope this could help others out there! You can download the script here.

10 thoughts on “Convert CSV to JSON using BASH

  1. Wow – that code is quite straight forward. My website json to csv converts the other way (json to csv) and the code behind it is a lot more complicated.

    Reply

  2. Thanks for your nice code. I have a lot of fun using it :-).

    Reply

  3. Hi Everyone ,

    I have .jason with Jason data , im using mac ,I want to convert data into csv using Unix terminal , can any one help me regarding this .

    Thanks
    Sumanth

    Reply

  4. Hi thanks a lot for this code it helped me a lot.

    i added Double Qouting for the head_array and i replaced the outer {} to [] so it is confom with RFC 7159 and ECMA 404.

    Cheers

    Andi

    Reply

  5. Hello,
    I am pretty beginner in the bash code,so please how can redirect the result to a specified folder,

    Many thanks,

    Reply

    1. In fact,the result file is all the time empty :/

      Win+R , cmd , i write “Nameofmyfile.sh input.csv > output.json”

      Reply

  6. Its Showing an empty file of .json
    error is: head_array[1]=”name” not found

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *