【问题标题】:jq cannot be tsv-formatted, only array errorjq 不能被 tsv 格式化,只有数组错误
【发布时间】:2018-07-23 16:57:16
【问题描述】:

我有这个结构

file.json:

{
"base_price_mw": 249.99, 
"best_offer_base_price": 280.06, 
"best_offer_nature": 11, 
"best_offer_promo_price": 247.35, 
"best_offer_shiping_price": 0, 
"best_shop_id": 2004, 
"best_shop_name": "Stuff", 
"cat_id": 69, 
"grey_dot": true, 
"is_exclusivity": null, 
"is_favorite": false, 
"is_new": false, 
"is_topsales": false, 
"manufacturer_id": 58, 
"name": "my product name", 
"nature_mw": 11, 
"note": "0.0000", 
"offers_count": 11, 
"offers_min_price": 233.21, 
"products_ids": 30671, 
"promo_price_mw": 249.99, 
"status": 1
}

我想用 jq 做成 tsv,但是 jq 说:

jq: error (at <stdin>:1): object ({"products_...) cannot be tsv-formatted, only array

我不明白为什么

我传递的完整命令是:

jq  '@tsv' file.json

我尝试了 -c 或 -r 和 -R 选项,但没有成功。我不明白为什么这不起作用 感谢您的帮助

【问题讨论】:

    标签: json csv jq


    【解决方案1】:

    应该是:

    jq -r 'to_entries|map(.value)|@tsv' file.json
    

    to_entries 将输入转换为:

    [
      {
        "key": "base_price_mw",
        "value": 249.99
      },
      {
        "key": "best_offer_base_price",
        "value": 280.06
      },
      {
        "key": "best_offer_nature",
        "value": 11
      },
      ...
    ]
    

    ...我们只从使用 map(.value) 的值中获取值并将其传递给 @tsv

    【讨论】:

    • 太棒了,我不知道为什么会这样,但这就像一个魅力。非常感谢
    • 大声笑,我喜欢这个评论......它总结了所有程序员的生活:我不知道为什么这样做,但这就像一个魅力
    【解决方案2】:

    如果你只想要值(不带标题):

    [.[]] | @tsv
    

    如果你也想要标题:

    (keys_unsorted, [.[]]) | @tsv
    

    【讨论】:

    • @hek2mgl - 太真实了! :-)
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 2019-12-06
    • 2019-01-26
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多