最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Using jq with the value of a variable as field value - Stack Overflow

programmeradmin0浏览0评论

I have a JSON file which I got via curl with hourly energy prices.

I can output the price for 6 o'clock with

jq '.energy | .todayHours | .[6] | .priceIncludingVat' < "$JSON"

I get the actual hour via date command: HOUR=$(date '+%-H')

How do I get the price for the actual hour? I have no clue how to insert the value of $HOUR into the jq query instead of the "6"

Thanks a lot!

Played around with different things I found e.g. with the "--arg" option but got nowhere, but maybe I tried them a wrong way :-(

I have a JSON file which I got via curl with hourly energy prices.

I can output the price for 6 o'clock with

jq '.energy | .todayHours | .[6] | .priceIncludingVat' < "$JSON"

I get the actual hour via date command: HOUR=$(date '+%-H')

How do I get the price for the actual hour? I have no clue how to insert the value of $HOUR into the jq query instead of the "6"

Thanks a lot!

Played around with different things I found e.g. with the "--arg" option but got nowhere, but maybe I tried them a wrong way :-(

Share Improve this question edited Feb 7 at 8:45 Barbaros Özhan 65.3k11 gold badges36 silver badges61 bronze badges asked Feb 6 at 20:21 X-FileX-File 531 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

As HOUR is a number, you can use --argjson instead of --arg. The (only) benefit is that you won't have to do the conversion from string to number yourself:

jq --argjson hour "$HOUR" '.energy.todayHours[$hour].priceIncludingVat' < "$JSON"
发布评论

评论列表(0)

  1. 暂无评论