Converting CSV to JSON in bash

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

    The right tool for this job is jq.

    jq -Rsn '
      {"occurrences":
        [inputs
         | . / "\n"
         | (.[] | select(length > 0) | . / ";") as $input
         | {"position": [$input[0], $input[1]], "taxo": {"espece": $input[2]}}]}
    ' 

    emits, given your input:

    {
      "occurences": [
        {
          "position": [
            "-21.3214077",
            "55.4851413"
          ],
          "taxo": {
            "espece": "Ruizia cordata"
          }
        },
        {
          "position": [
            "-21.3213078",
            "55.4849803"
          ],
          "taxo": {
            "espece": "Cossinia pinnata"
          }
        }
      ]
    }
    

    By the way, a less-buggy version of your original script might look like:

    #!/usr/bin/env bash
    
    items=( )
    while IFS=';' read -r lat long pos _; do
      printf -v item '{ "position": [%s, %s], "taxo": {"espece": "%s"}}' "$lat" "$long" "$pos"
      items+=( "$item" )
    done 

    Note:

    • There's absolutely no point using cat to pipe into a loop (and good reasons not to); thus, we're using a redirection (<) to open the file directly as the loop's stdin.
    • read can be passed a list of destination variables; there's thus no need to read into an array (or first to read into a string, and then to generate a heresting and to read from that into an array). The _ at the end ensures that extra columns are discarded (by putting them into the dummy variable named _) rather than appended to pos.
    • "${array[*]}" generates a string by concatenating elements of array with the character in IFS; we can thus use this to ensure that commas are present in the output only when they're needed.
    • printf is used in preference to echo, as advised in the APPLICATION USAGE section of the specification for echo itself.
    • This is still inherently buggy since it's generating JSON via string concatenation. Don't use it.

提交回复
热议问题