Converting CSV to JSON in bash

前端 未结 9 1874
梦毁少年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:48

    In general, if your jq has the inputs built-in filter (available since jq 1.5), then it is better to use it rather than the -s command-line option.

    Here in any case is a solution using inputs. This solution is also variable-free.

    {"occurrences":
      [inputs
       | select(length > 0)
       | . / ";"
       | {"position": [.[0], .[1]], 
          "taxo": {"espece": .[2]}} ]}
    

    SSV, CSV, and all that

    The above of course assumes that the file has semicolon-separated fields in each line, and that there are none of the complications associated with CSV files.

    If the input has fields that are strictly delimited by a single character, then jq should have no problems handling it. Otherwise, it might be best to use a tool that can reliably convert to the TSV (tab-separated value) format, which jq can handle directly.

提交回复
热议问题