Я искал, как преобразовать файл json
, используя jq
, который я получаю от веб-службы, чтобы удалить уровни глубины и сохранить только некоторые значения.
Файл 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?