Converting CSV to JSON in bash

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

    For completeness sake, Xidel together with some XQuery magic can do this too:

    xidel -s input.csv --xquery '
      {
        "occurrences":for $x in tokenize($raw,"\n") let $a:=tokenize($x,";") return {
          "position":[
            $a[1],
            $a[2]
          ],
          "taxo":{
            "espece":$a[3]
          }
        }
      }
    '
    
    {
      "occurrences": [
        {
          "position": ["-21.3214077", "55.4851413"],
          "taxo": {
            "espece": "Ruizia cordata"
          }
        },
        {
          "position": ["-21.3213078", "55.4849803"],
          "taxo": {
            "espece": "Cossinia pinnata"
          }
        }
      ]
    }
    

提交回复
热议问题