【问题标题】:JQ show empty or null values and iterate over different keysJQ 显示空值或空值并遍历不同的键
【发布时间】:2020-04-14 15:16:39
【问题描述】:

我正在研究如何使用从 Web 服务收到的 jq 转换 json 文件,以删除深度级别并仅保留一些值。

https://jqplay.org/s/_RZNr_oYwE

Json 文件类似这样:

[
  {
   "date":"2020-04-13",
   "product1":{"p_max":18.00,"p_min":8.00,"interval":[{"time":"06:00:00","price":8.00},{"time":"12:00:00","price":18.00},{"time":"18:00:00","price":16.00},{"time":"24:00:00","price":9.00}]},
   "product2":{"p_max":18.00,"p_min":8.00,"interval":[{"time":"06:00:00","price":8.00},{"time":"12:00:00","price":18.00},{"time":"18:00:00","price":16.00},{"time":"24:00:00","price":9.00}]}
  },
  {
   "date":"2020-04-14",
   "product1":{"p_max":18.00,"p_min":8.00,"interval":[{"time":"06:00:00","price":8.00},{"time":"12:00:00","price":18.00},{"time":"18:00:00","price":16.00},{"time":"24:00:00","price":9.00}]},
   "product2":{"p_max":18.00,"p_min":8.00,"interval":[{"time":"06:00:00","price":9.00},{"time":"12:00:00","price":16.00},{"time":"18:00:00","price":15.00},{"time":"24:00:00","price":11.00}]}
  },
  {
   "date":"2020-04-15",
   "product1":{"p_max":16.00,"p_min":9.00,"interval":[]},
   "product2":{"p_max":16.00,"p_min":9.00,"interval":[]}
  }
]

我想出了如何转换 json,但我错过了一些值:

jq --compact-output '.[] | .product1.interval[] as $interval| {date: .date, time: ($interval.time//"N/A"), price: ($interval.price//-1)}'

这是输出:

{"date":"2020-04-13", "time":"06:00:00", "price":8}
{"date":"2020-04-13", "time":"12:00:00", "price":18}
{"date":"2020-04-13", "time":"18:00:00", "price":16}
{"date":"2020-04-13", "time":"24:00:00", "price":9}
{"date":"2020-04-14", "time":"06:00:00", "price":9}
{"date":"2020-04-14", "time":"12:00:00", "price":16}
{"date":"2020-04-14", "time":"18:00:00", "price":15}
{"date":"2020-04-14", "time":"24:00:00", "price":11}

但我想念:

{"date":"2020-04-15", "time":"N/A", "price":-1}

我也尝试过,但不能,所以我想我将不得不在 bash 脚本中进行迭代以获得以下输出:

{"date":"2020-04-13", "time":"06:00:00", "product": "product1", "price":8}
{"date":"2020-04-13", "time":"06:00:00", "product": "product2", "price":4}
{"date":"2020-04-13", "time":"12:00:00", "product": "product1", "price":18}
{"date":"2020-04-13", "time":"12:00:00", "product": "product2", "price":8]
{"date":"2020-04-13", "time":"18:00:00", "product": "product1", "price":16}
{"date":"2020-04-13", "time":"18:00:00", "product": "product2", "price":8}
{"date":"2020-04-13", "time":"24:00:00", "product": "product1", "price":9}
{"date":"2020-04-13", "time":"24:00:00", "product": "product2", "price":5}
{"date":"2020-04-14", "time":"06:00:00", "product": "product1", "price":9}
{"date":"2020-04-14", "time":"06:00:00", "product": "product2", "price":6}
{"date":"2020-04-14", "time":"12:00:00", "product": "product1", "price":16}
{"date":"2020-04-14", "time":"12:00:00", "product": "product2", "price":6}
{"date":"2020-04-14", "time":"18:00:00", "product": "product1", "price":15}
{"date":"2020-04-14", "time":"18:00:00", "product": "product2", "price":15}
{"date":"2020-04-14", "time":"24:00:00", "product": "product1", "price":11}
{"date":"2020-04-14", "time":"24:00:00", "product": "product2", "price":11}
{"date":"2020-04-15", "time":"N/A",      "product": "product1", "price":-1}
{"date":"2020-04-15", "time":"N/A",      "product": "product2", "price":-1}

有没有办法获得“空”值? 有什么方法可以用 jq 生成第二个输出而不需要在 bash 上进行迭代?

【问题讨论】:

    标签: json bash jq


    【解决方案1】:

    不,您不需要在 bash 上迭代,JQ 可以自己完成。

    使用keys_unsorted 获取产品名称(这需要删除date)并使用if-then-else 表达式将空的interval[{"time": "N/A", "price": -1}] 交替。

    .[]
    | {date} + (
      del(.date)
      | keys_unsorted[] as $product
      | {$product} + (
        .[$product].interval
        | if . == [] then
            {time: "N/A", price: -1}
          else
            .[]
          end
      )
    )
    

    Online demo

    【讨论】:

    • 谢谢。我尝试了类似的方法,但现在我发现它失败了,因为在 .[$product].interval 块之后我需要添加 | .[] 以使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2013-12-20
    相关资源
    最近更新 更多