I would like to parse a JSON file to get data in variables using a bash script.
At this moment I use jq to format the file, and grep to get the data:
temperature=$(grep <weer.json -B2 -A24 "6391" | grep '"temperature"' | grep -o '[-.0-9]*')
This is of course an amateuristic, dirty way, but it works. How can I get all this data into variables using just jq? Is it possible?
{
"$id": "1",
"buienradar": {
"$id": "2",
"copyright": "(C)opyright Buienradar / RTL. Alle rechten voorbehouden",
"terms": "Deze feed mag vrij worden gebruikt onder voorwaarde van bronvermelding buienradar.nl inclusief een hyperlink naar https://www.buienradar.nl. Aan de feed kunnen door gebruikers of andere personen geen rechten worden ontleend."
},
"actual": {
"$id": "3",
"actualradarurl": "https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512",
"sunrise": "2021-02-04T08:15:00",
"sunset": "2021-02-04T17:32:00",
"stationmeasurements": [
{
"$id": "4",
"stationid": 6391,
"stationname": "Meetstation Arcen",
"lat": 51.5,
"lon": 6.2,
"regio": "Venlo",
"timestamp": "2021-02-04T11:20:00",
"weatherdescription": "Zwaar bewolkt",
"iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
"graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
"winddirection": "WZW",
"temperature": 7,
"groundtemperature": 6.6,
"feeltemperature": 5.6,
"windgusts": 5,
"windspeed": 2.1,
"windspeedBft": 2,
"humidity": 86,
"precipitation": 0,
"sunpower": 108,
"rainFallLast24Hour": 9.9,
"rainFallLastHour": 0,
"winddirectiondegrees": 246
},
I've tried cat weer.json | jq '.actual.stationmeasurements'
, but how can I extract only, for example, the temperature for stationid 6391?
cat weer.json | jq '.actual.stationmeasurements.stationid[6260]'
results in an errormessage:
jq: error (at <stdin>:1324): Cannot index array with string "stationid"
Source for the json file: https://data.buienradar.nl/2.0/feed/json
Thanks!