raylight
raylight

Reputation: 727

How can I erase specific parts of an array based on their content using jq?

Let's say I have the following GeoJSON file:

{
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {
        "algorithm": "parameterA"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": []
        }
      },
      {
        "type": "Feature",
        "properties": {
        "algorithm": "parameterB"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": []
        }
      },
      {
        "type": "Feature",
        "properties": {
        "algorithm": "parameterB"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": []
        }
      },
      {
        "type": "Feature",
        "properties": {
        "algorithm": "parameterA"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": []
        }
      }
    ],
    "properties": {
      "name": "myGeoJSON"
    }
}

I'd like to find a way of erasing only the arrays containing a specific algorithm key. Let's say I want to erase all arrays which algorithm is equal to parameterA, so I'd get the following result:

{
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "properties": {
        "algorithm": "parameterB"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": []
        }
      },
      {
        "type": "Feature",
        "properties": {
        "algorithm": "parameterB"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": []
        }
      }
    ],
    "properties": {
      "name": "myGeoJSON"
    }
}

I know I can erase a specific value from my array by using something like jq del'(.features[0] )' file.json. But I don't know how to use a condition in it that will erase only array blocks that have a specific property. How can I do it using jq?

Upvotes: 0

Views: 42

Answers (1)

ikegami
ikegami

Reputation: 386396

jq 'del( .features[] | select(.properties.algorithm == "parameterA") )'

jqplay

Upvotes: 1

Related Questions