0

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!

wtrdk
  • 131
  • 2
  • 12
  • Where is your combination of `jq` and `grep`? Your example code just uses `grep`. – Barmar Feb 04 '21 at 10:49
  • This is exactly the kind of task that `jq` is for, you shouldn't need `grep`. – Barmar Feb 04 '21 at 10:51
  • I'm sorry, I only use ```jq``` to format the file. I've changed my question. I know it is possible to use JQ for this, but I don't exactly know how... I've been reading several tutorials (like this one: http://www.compciv.org/recipes/cli/jq-for-parsing-json/ ) but I just can't get it done... – wtrdk Feb 04 '21 at 10:53
  • Show your best attempt and someone will help you fix it. – Barmar Feb 04 '21 at 10:54
  • Thanks, I've updated my question. Hope it meets the standards now! – wtrdk Feb 04 '21 at 11:00
  • I already gave you a link to another question that shows how to select by the value of a property. – Barmar Feb 04 '21 at 11:01
  • 1
    Something like `jq '.actual.stationmeasurements | select(.stationid=6260) | .temperature'` – Barmar Feb 04 '21 at 11:03
  • Thanks! When I try that I get ```jq: error (at weer.json:1324): Cannot index array with string "stationid"``` – wtrdk Feb 04 '21 at 11:08
  • I'm not a jq expert, sorry. But I thought that would get you in the right direction. You should be able to find more recipes in the web page linked from the other question. – Barmar Feb 04 '21 at 11:13
  • Thanks for your effort, much appreciated! – wtrdk Feb 04 '21 at 11:14
  • Thanks for getting me on the right track. I worked it out, the solution is as follows: ```jq weer.json '.actual.stationmeasurements[] | select(.stationid==6260) | .temperature'``` – wtrdk Feb 04 '21 at 12:47

0 Answers0