4

Hello I've been using this:

curl -s "http://api.openweathermap.org/data/2.5/forecast?id=6361046&APPID=6be5e3a6e62680c28044791e8fc7b568&units=metric" -o ~/.cache/weather.json

to write a weather.json file in my cache folder. the output is this. the problem is that it's only in 1 line and I'm trying to use grep to get an output of the icon value of today:

{"cod":"200","message":0,"cnt":40,"list":[{"dt":1574629200,"main":{"temp":12.8,"temp_min":12.8,"temp_max":13.07,"pressure":1020,"sea_level":1020,"grnd_level":1012,"humidity":82,"temp_kf":-0.27},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}]

which in this case is "04n" so I can use something like this:

~/.weather-icons/$(grep "icon" ~/.cache/weather.json).png

that would output ~/.weather-icons/04n.png so I can use the icon from my folder with the same name as the icon value to display in conky using $image. My question is how can i get this done? I don't know if there is a better command to read a file.json using the parameters shown here or if there is a way I can use grep to just output the icon value. Thanks

Wonky
  • 331

2 Answers2

8

IMO the right way to do this (as suggested in a comment by @fedonkadifeli ) is to use a JSON-aware tool to select the list element with your desired timestamp.

For example, the first timestamp 1574629200 represents today, 24th November 2019 16:00 EST and using jq on the saved weather.json file:

$ jq -r --arg dt "$(date +%s -d 'today 16:00:00 EST')" '
    .list[] | select( .dt == ($dt | tonumber) ) | .weather[].icon
  ' weather.json
04n

If you don't need the weather data for other purposes, you could consider piping the curl output straight to the jq command rather than saving it to a file.

If you don't care about a particular time of day, you can get the first icon even more simply

curl ... | jq -r '.list[0].weather[].icon'
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • You gotta know I've been strugling to use jq right, I was always trying jq '.weather.icon' to get the value i wanted. Thanks for giving some insight on how to read json files properly using jq. Gonna try and improve the code to make it cleaner using jq. – Wonky Nov 24 '19 at 23:11
  • Upvoting because it's the answer I should have written if the day had 25 hours. – WinEunuuchs2Unix Nov 24 '19 at 23:40
2

A better answer can probably be made using a tool like jq but a brute force method would be:

$ head -c400 ~/.cache/weather.xml

{"cod":"200","message":0,"cnt":40,"list":[{"dt":1574629200,"main":{"temp":12.8,"temp_min":12.8,"temp_max":13.07,"pressure":1020,"sea_level":1020,"grnd_level":1012,"humidity":82,"temp_kf":-0.27},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":2.22,"deg":232},"sys":{"pod":"n"},"dt_txt":"2019-11-24 21:00:00"},{"dt":1574640000,"ma

This displays the first 400 characters of the file and it appears your information is always at the start of the file?

$ head -c400 ~/.cache/weather.xml | sed 's/.*icon":"//g'

04n"}],"clouds":{"all":92},"wind":{"speed":2.22,"deg":232},"sys":{"pod":"n"},"dt_txt":"2019-11-24 21:00:00"},{"dt":1574640000,"ma

This removes everything up to and including "icon":" leaving 04n which you desire at the beginning of the line.

$ head -c400 ~/.cache/weather.xml | sed 's/.*icon":"//g' | sed 's/".*//'

04n

Finally this gives you everything you were looking for.

Not pretty but it works.