簡體   English   中英

JQ 顯示空值或 null 值並迭代不同的鍵

[英]JQ show empty or null values and iterate over different keys

我正在研究如何使用從 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)}'

這是 output:

{"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 腳本中迭代以獲得以下 output:

{"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 生成第二個 output 而無需迭代 bash?

不,您不需要迭代 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
  )
)

在線演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM