根据特定键

时间:2017-02-18 10:08:44

标签: filter geojson jq

我尝试编辑geojson文件,只保留具有键" name"的对象。 过滤器有效但我无法找到保留其他对象的方法,特别是几何体并将整个内容重定向到新的geojson文件。有没有办法在过滤其中一个子对象后显示整个对象?

以下是我的数据示例。第一个对象有" name"财产和第二个没有&#t; t:

{
  "features": [
    {
      "type": "Feature",
      "id": "way/24824633",
      "properties": {
        "@id": "way/24824633",
        "highway": "tertiary",
        "lit": "yes",
        "maxspeed": "50",
        "name": "Rue de Kleinbettingen",
        "surface": "asphalt"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            5.8997935,
            49.6467825
          ],
          [
            5.8972561,
            49.6467445
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "id": "way/474396855",
      "properties": {
        "@id": "way/474396855",
        "highway": "path"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            5.8020608,
            49.6907648
          ],
          [
            5.8020695,
            49.6906054
          ]
        ]
      }
    }
  ]
}

这是我尝试过的,使用 jq

cat file.geojson | jq '.features[].properties | select(has("name"))'

"几何"也是"功能"的孩子。但我无法直接从"功能"中找到一种方法来进行选择。水平。有办法做到这一点吗?或者是解决方案的更好途径?

所以,所需的输出是:

{
  "type": "Feature",
  "id": "way/24824633",
  "properties": {
    "@id": "way/24824633",
    "highway": "tertiary",
    "lit": "yes",
    "maxspeed": "50",
    "name": "Rue de Kleinbettingen",
    "surface": "asphalt"
  },
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [
        5.8997935,
        49.6467825
      ],
      [
        5.8972561,
        49.6467445
      ]
 ]}}

1 个答案:

答案 0 :(得分:3)

您可以将已过滤的列表assign返回.features

jq '.features |= map(select(.properties|has("name")))'