How to map an object to arrays so it can be converted to csv?

前端 未结 5 1654
抹茶落季
抹茶落季 2020-12-24 06:13

I\'m trying to convert an object that looks like this:

{
  \"123\" : \"abc\",
  \"231\" : \"dbh\",
  \"452\" : \"xyz\"
}

To csv that looks

相关标签:
5条回答
  • 2020-12-24 06:46

    Try this filter:

    to_entries[] | [.key, .value]
    
    • to_entries converts an object to an array of key/value objects. [] breaks up the array to each of the items in the array
    • then for each of the items, covert to an array containing the key and value.

    This produces the following output:

    [
      "123",
      "abc"
    ],
    [
      "231",
      "dbh"
    ],
    [
      "452",
      "xyz"
    ]
    

    Then you can use the @csv filter to convert the rows to CSV rows.

    $ echo '{"123":"abc","231":"dbh","452":"xyz"}' | jq -r 'to_entries[] | [.key, .value] | @csv'
    "123","abc"
    "231","dbh"
    "452","xyz"
    
    0 讨论(0)
  • 2020-12-24 06:50

    Jeff answer is a good starting point, something closer to what you expect:

    cat input.json | jq 'to_entries | map([.key, .value]|join(","))'
    
    [
     "123,abc",
     "231,dbh",
     "452,xyz"
    ]
    

    But did not find a way to join using newline:

    cat input.json | jq 'to_entries | map([.key, .value]|join(","))|join("\n")'
    
    "123,abc\n231,dbh\n452,xyz"
    
    0 讨论(0)
  • 2020-12-24 06:52

    Here's an example I ended up using this morning (processing PagerDuty alerts):

    cat /tmp/summary.json | jq -r '
      .incidents
      | map({desc: .trigger_summary_data.description, id:.id})
      | group_by(.desc)
      | map(length as $len
      | {desc:.[0].desc, length: $len}) 
      | sort_by(.length) 
      | map([.desc, .length] | @csv)
      | join("\n") '
    

    This dumps a CVS-separated document that looks something like: "[Triggered] Something annoyingly frequent",31 "[Triggered] Even more frequent alert!",35 "[No data] Stats Server is probably acting up",55

    0 讨论(0)
  • 2020-12-24 06:55

    Try This give same output you want

    echo '{"123":"abc","231":"dbh","452":"xyz"}' | jq -r 'to_entries | .[] | "\"" + .key + "\",\"" + (.value | tostring)+ "\""'
    
    0 讨论(0)
  • 2020-12-24 07:07
    onecol2txt () {
     awk 'BEGIN { RS="_end_"; FS="\n"}
       { for (i=2; i <= NF; i++){
           printf "%s ",$i 
           }
         printf "\n" 
       }'
    }
    cat jsonfile | jq -r -c '....,"_end_"' | onecol2txt
    
    0 讨论(0)
提交回复
热议问题