【问题标题】:jq - json list of dicts to csvjq - 到 csv 的 json 字典列表
【发布时间】:2019-12-17 20:26:10
【问题描述】:

输入 JSON 示例:

[
  {"name":"Θεμιστοκλής","surname":"Παπαϊωάννου","gender":"male","region":"Greece"}, 
  {"name":"Casian","surname":"Cusin","gender":"male","region":"Romania"}
]

现在我的代码输出为字符串:

$ curl -s https://uinames.com/api/?amount=3 | jq '.[] | [.name, .surname] | @csv'
"\"Anamaria\",\"Tămaș\""
"\"Aurora\",\"Coronado\""
"\"Εύηνος\",\"Ελευθερόπουλος\""

我需要什么:

"Anamaria","Tămaș"
"Aurora","Coronado"
"Εύηνος","Ελευθερόπουλος"

谁能解释一下我做错了什么?

【问题讨论】:

    标签: json csv export-to-csv jq


    【解决方案1】:

    您应该在调用 jq 时添加 -r 命令行选项。

    例子:

    $ curl -Ss 'https://uinames.com/api/?amount=3' |
       jq -r '.[] | [.name, .surname] | @csv' 
    "Γόργασος","Θεοδωρίδης"
    "Marta","Brediceanu"
    "Iulian","Bârcă"
    

    【讨论】:

      猜你喜欢
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2021-12-29
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多