Skip to content

Instantly share code, notes, and snippets.

@nmagee
Last active March 21, 2022 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 jbruna0@ask.com 147.124.85.108 07/02/2000
2 Bernice Maior bmaior1@hibu.com 159.254.81.36 12/22/1991
3 Freeman Michel fmichel2@samsung.com 2.86.134.76 07/18/1992
4 Fiona Purle fpurle3@ihg.com 17.63.61.160 11/08/1985
5 Sansone Marchetti smarchetti4@shinystat.com 49.23.160.174 07/22/1994
6 Darell Crut dcrut5@businesswire.com 31.4.110.69 02/11/1986
7 Ira Cron icron6@irs.gov 175.6.167.0 11/27/1980
8 Stewart Blewitt sblewitt7@pinterest.com 21.6.120.53 06/22/1988
9 Ardelia Sealey asealey8@statcounter.com 22.162.108.33 12/09/1985
10 Roddy Licciardiello rlicciardiello9@senate.gov 207.118.76.239 01/12/1985
11 Mariette Berget mbergeta@admin.ch 227.110.15.170 11/01/1986
12 Ondrea Cleugh ocleughb@yellowbook.com 54.221.136.115 09/25/1980
13 Roseanna Tuppeny rtuppenyc@washingtonpost.com 45.179.99.188 03/23/1985
14 Carolee MacSweeney cmacsweeneyd@deliciousdays.com 188.83.176.26 10/24/1992
15 Katerine Blazy kblazye@sourceforge.net 122.122.17.199 07/22/1981
16 Zorah Saph zsaphf@exblog.jp 111.127.8.73 02/18/1988
17 Brew Revitt brevittg@about.com 52.173.236.55 01/04/1982
18 Selestina MacGillespie smacgillespieh@msn.com 77.235.40.19 07/09/1995
19 Marilin Inkin minkini@cdc.gov 113.13.97.161 09/30/1992
20 Celesta Baskeyfield cbaskeyfieldj@economist.com 253.238.100.213 08/26/1991
21 Cobby Dorow cdorowk@pbs.org 207.245.237.33 01/29/1985
22 Abbye Hick ahickl@state.gov 25.246.238.226 09/26/1986
23 Dulcinea Mattsson dmattssonm@ihg.com 238.60.156.235 07/24/1989
24 Alano Tapp atappn@ebay.com 76.114.12.37 03/05/1982
25 Mollee Coweuppe mcoweuppeo@hexun.com 204.64.87.29 05/23/1980
26 Eilis Kopke ekopkep@123-reg.co.uk 147.129.140.115 06/26/1991
27 Ashia Bulter abulterq@fema.gov 229.67.5.181 06/04/2000
28 Lynnet Clelland lclellandr@statcounter.com 29.243.65.253 06/10/1987
29 Gabrila Wildman gwildmans@issuu.com 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