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

前端 未结 5 1653
抹茶落季
抹茶落季 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: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"
    

提交回复
热议问题