jq - Filter by properties and case insensitive

Doing some zen like programming tasks by patching together utilities and pipes for a task of taking a GeoJSON dataset and finding all the elements of a particular county.

Example data set format

{
    "type": "FeatureCollection",
    "name": "Weather_Stations",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "OBJECTID": 1,
                "STNSNAME": "ABERDEEN EXP STN",
                "STNSCODE": "10",
                "AGENCY": "NWS",
                "COUNTY": "BINGHAM",
                "LONGITUDE": "-112 50 00",
                "LATITUDE": "42 57 00",
                "ELEVATION": 4410.0
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -112.834171929999968,
                    42.949917913000036
                ]
            }
        },
....

Now use jq to filter the and find all the features that are in the county Twin Falls in this case the input file I am using is a public dataset from Idaho of weather stations that I have downloaded and named idaho_stations.geojson, also providing the direct download into memory manipulation

Full command

wget -qO- https://geocatalog-uidaho.hub.arcgis.com/datasets/255e34e3e1ac4cd584f6821358856c52_0.geojson?outSR=%7B%22latestWkid%22%3A8826%2C%22wkid%22%3A102605%7D | jq -r '.features ' | jq -c '.[] | select (.properties.COUNTY | test("Twin Falls"; "i"))' | jq --slurp

Or direct file access

cat idaho_stations.geojson | jq -r '.features ' | jq -c '.[] | select (.properties.COUNTY | test("Twin Falls"; "i"))' | jq --slurp

Explanation

  • wget -qO- url - To download the linked file in quiet mode to standard output for manipulation
  • cat - to load the file into memory
  • jq -r '.features' - Filter the features properties into raw output
  • jq -c '.[] | select (.properties.COUNTY | test ("Twin Falls"; "i"))' - Select the input nodes in compact mode and then selecting the property properties->COUNTY, run a case insensitive test against it for Twin Falls
  • jq – slurp - Now take the compacted result and format it back to pretty JSON

Results

[
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 28,
      "STNSNAME": "BUHL 2",
      "STNSCODE": "1220",
      "AGENCY": "NWS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-114 45 00",
      "LATITUDE": "42 36 00",
      "ELEVATION": 3800
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -114.75089584099999,
        42.59991073500004
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 34,
      "STNSNAME": "CASTLEFORD 2 N",
      "STNSCODE": "1551",
      "AGENCY": "NWS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-114 52 00",
      "LATITUDE": "42 33 00",
      "ELEVATION": 3830
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -114.86756650299998,
        42.54991133100003
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 90,
      "STNSNAME": "HOLLISTER",
      "STNSCODE": "4295",
      "AGENCY": "NWS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-114 34 00",
      "LATITUDE": "42 21 00",
      "ELEVATION": 4525
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -114.56755118099994,
        42.34991669900006
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 119,
      "STNSNAME": "MAGIC MOUNTAIN SNOTEL",
      "STNSCODE": "14G02S",
      "AGENCY": "NRCS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-114 18 00",
      "LATITUDE": "42 11 00",
      "ELEVATION": 6880
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -114.30087213899998,
        42.18325255600007
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 202,
      "STNSNAME": "TWIN FALLS KVMT",
      "STNSCODE": "9293",
      "AGENCY": "NWS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-114 28 00",
      "LATITUDE": "42 35 00",
      "ELEVATION": 3670
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -114.46755079099995,
        42.583249899000066
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 203,
      "STNSNAME": "TWIN FALLS WSO",
      "STNSCODE": "9303",
      "AGENCY": "NWS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-114 21 00",
      "LATITUDE": "42 33 00",
      "ELEVATION": 3960
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -114.35087931799995,
        42.54991871500005
      ]
    }
  },
  {
    "type": "Feature",
    "properties": {
      "OBJECTID": 211,
      "STNSNAME": "WILSON CREEK",
      "STNSCODE": "15G02S",
      "AGENCY": "NRCS",
      "COUNTY": "TWIN FALLS",
      "LONGITUDE": "-115 01 00",
      "LATITUDE": "42 01 00",
      "ELEVATION": 7500
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        -115.01756275099996,
        42.01658178000008
      ]
    }
  }
]