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
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 usingjq
on the savedweather.json
file:If you don't need the weather data for other purposes, you could consider piping the
curl
output straight to thejq
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
A better answer can probably be made using a tool like
jq
but a brute force method would be:This displays the first 400 characters of the file and it appears your information is always at the start of the file?
This removes everything up to and including
"icon":"
leaving04n
which you desire at the beginning of the line.Finally this gives you everything you were looking for.
Not pretty but it works.