如何使用jq json解析器获取整个父节点?

时间:2018-09-26 10:47:29

标签: json object parent jq

我正在尝试在json文件中找到一个值,因此我需要获取整个json数据而不是该特定块。

这是我的示例json

[{
      "name" : "Redirect to Website 1",

      "behaviors" : [ {
        "name" : "redirect",
        "options" : {
          "mobileDefaultChoice" : "DEFAULT",
          "destinationProtocol" : "HTTPS",
          "destinationHostname" : "SAME_AS_REQUEST",
          "responseCode" : 302
        }
      } ],
      "criteria" : [ {
        "name" : "requestProtocol",
        "options" : {
          "value" : "HTTP"
        }
      } ],
      "criteriaMustSatisfy" : "all"
},
{
      "name" : "Redirect to Website 2",

      "behaviors" : [ {
        "name" : "redirect",
        "options" : {
          "mobileDefaultChoice" : "DEFAULT",
          "destinationProtocol" : "HTTPS",
          "destinationHostname" : "SAME_AS_REQUEST",
          "responseCode" : 301
        }
      } ],
      "criteria" : [ {
        "name" : "contentType",
        "options" : {
          "matchOperator" : "IS_ONE_OF",
          "values" : [ "text/html*", "text/css*", "application/x-javascript*" ],
        }
      } ],
      "criteriaMustSatisfy" : "all"
}]

我正在尝试在每个"name" : "redirect"数组中匹配behaviors,如果匹配,则需要包括“条件”部分的整个块,如您在同一块{{1 }}

我设法使用选择方法找到值,但无法获取父节。

https://jqplay.org/s/BWJwVdO3Zv

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以尝试以下jq命令:

floatingActionButton: new FloatingActionButton(
  onPressed: addCard, // pass method reference here
  tooltip: 'Add Card',
  child: new Icon(Icons.add),
)

答案 1 :(得分:1)

为避免不必要的重复:

.[]
| first(select(.behaviors[].name == "redirect"))

等效地:

.[]
| select(any(.behaviors[]; .name == "redirect"))