Converting CSV to JSON in bash

前端 未结 9 1869
梦毁少年i
梦毁少年i 2021-02-04 03:12

Trying to convert a CSV file into a JSON

Here is two sample lines :

-21.3214077;55.4851413;Ruizia cordata
-21.3213078;55.4849803;Cossinia pinnata
         


        
9条回答
  •  不思量自难忘°
    2021-02-04 03:28

    Here is an article on the subject: https://infiniteundo.com/post/99336704013/convert-csv-to-json-with-jq

    It also uses JQ, but a bit different approach using split() and map().

    jq --slurp --raw-input \
       'split("\n") | .[1:] | map(split(";")) |
          map({
             "position": [.[0], .[1]],
             "taxo": {
                 "espece": .[2]
              }
          })' \
      input.csv > output.json
    

    It doesn't handle separator escaping, though.

提交回复
热议问题