jq,根据属性值

时间:2017-10-25 19:28:55

标签: unique jq

我是新手, 我输入了json:

{
  "2017-09-15": {
    "key": "stops",
    "node": {
      "lastUpdate": "2017-09-15 05:15:26",
      "stops": [
        {
          "stopId": 11000,
          "stopCode": null,
          "stopName": null,
          "stopShortName": "1000",
          "stopDesc": "Tom Tail",
          "subName": "1000",
          "zoneId": null,
          "zoneName": null,
          "stopUrl": "",
          "locationType": null,
          "parentStation": null,
          "stopTimezone": "",
          "wheelchairBoarding": null,
          "virtual": null,
          "nonpassenger": null,
          "depot": null,
          "ticketZoneBorder": null,
          "onDemand": null,
          "activationDate": "2017-09-14"
        },
        {
          "stopId": 1100,
          "stopCode": "04",
          "stopName": "One Three",
          "stopShortName": "1100",
          "stopDesc": "Big Wall",
          "subName": "04",
          "zoneId": 1,
          "zoneName": "Demo2",
          "stopUrl": "",
          "locationType": null,
          "parentStation": null,
          "stopTimezone": "",
          "wheelchairBoarding": null,
          "virtual": 0,
          "nonpassenger": 0,
          "depot": 0,
          "ticketZoneBorder": 0,
          "onDemand": 0,
          "activationDate": "2017-09-24"
        }
      ]
    }
  }
}

我可以通过过滤器获得所有停靠点: jq -r'。[]。node.stops'json

但是如何搜索特定的停止,例如基于stopDescr - 确切名称还是包含字符串?

THX。

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上......

.[].node.stops[] | select(.stopDesc == "Big Wall")

独特

关于补充质询:

1)sortunique都希望数组作为输入

2)调用unique无需拨打sort

3)unique生成一个数组,因此您可能希望使用unique[]

4)将(JSON)值流转换为数组的一种方法是将生成流的表达式包装在方括号中。

以下是一个例子:

[.[].node.stops[] | select(.stopDesc == "Big Wall") | .zoneName]
| unique