Skip to content

Instantly share code, notes, and snippets.

@nmagee
Last active March 21, 2022 22:22
Show Gist options
  • Save nmagee/f22b0f5b604af15b6cc1a13a9ebf68dc to your computer and use it in GitHub Desktop.
Save nmagee/f22b0f5b604af15b6cc1a13a9ebf68dc to your computer and use it in GitHub Desktop.
Converts CSV into JSON
#!/bin/bash
jq --slurp --raw-input --raw-output \
'split("\n") | .[1:] | map(split(",")) |
map({"id": .[0],
"fname": .[1],
"lname": .[2],
"email": .[3],
"ipv4": .[5]})' \
mock_data.csv > mock_data.json
id first_name last_name email ip_address dob
1 Jereme Bruna [email protected] 147.124.85.108 07/02/2000
2 Bernice Maior [email protected] 159.254.81.36 12/22/1991
3 Freeman Michel [email protected] 2.86.134.76 07/18/1992
4 Fiona Purle [email protected] 17.63.61.160 11/08/1985
5 Sansone Marchetti [email protected] 49.23.160.174 07/22/1994
6 Darell Crut [email protected] 31.4.110.69 02/11/1986
7 Ira Cron [email protected] 175.6.167.0 11/27/1980
8 Stewart Blewitt [email protected] 21.6.120.53 06/22/1988
9 Ardelia Sealey [email protected] 22.162.108.33 12/09/1985
10 Roddy Licciardiello [email protected] 207.118.76.239 01/12/1985
11 Mariette Berget [email protected] 227.110.15.170 11/01/1986
12 Ondrea Cleugh [email protected] 54.221.136.115 09/25/1980
13 Roseanna Tuppeny [email protected] 45.179.99.188 03/23/1985
14 Carolee MacSweeney [email protected] 188.83.176.26 10/24/1992
15 Katerine Blazy [email protected] 122.122.17.199 07/22/1981
16 Zorah Saph [email protected] 111.127.8.73 02/18/1988
17 Brew Revitt [email protected] 52.173.236.55 01/04/1982
18 Selestina MacGillespie [email protected] 77.235.40.19 07/09/1995
19 Marilin Inkin [email protected] 113.13.97.161 09/30/1992
20 Celesta Baskeyfield [email protected] 253.238.100.213 08/26/1991
21 Cobby Dorow [email protected] 207.245.237.33 01/29/1985
22 Abbye Hick [email protected] 25.246.238.226 09/26/1986
23 Dulcinea Mattsson [email protected] 238.60.156.235 07/24/1989
24 Alano Tapp [email protected] 76.114.12.37 03/05/1982
25 Mollee Coweuppe [email protected] 204.64.87.29 05/23/1980
26 Eilis Kopke [email protected] 147.129.140.115 06/26/1991
27 Ashia Bulter [email protected] 229.67.5.181 06/04/2000
28 Lynnet Clelland [email protected] 29.243.65.253 06/10/1987
29 Gabrila Wildman [email protected] 133.42.235.188 01/17/1991
@nmagee
Copy link
Author

nmagee commented Feb 23, 2021

Works fine but has a final record of null values.

@jmtscaff
Copy link

@nmagee Changing the range to [1:-1] will remove the last null value, let me know if it works

#!/bin/bash

jq --slurp --raw-input --raw-output \
  'split("\n") | .[1:-1] | map(split(",")) |
      map({"id": .[0],
           "fname": .[1],
           "lname": .[2],
           "email": .[3],
           "ipv4": .[5]})' \
  mock_data.csv > mock_data.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment