当对象名称是URL时查询

时间:2018-12-22 07:48:54

标签: json select key jq

我编写了一个JQ搜索,输出以下内容,但是我无法弄清楚如何深入细节并从中提取特定信息。

{
  "https://www.example.org/rest/relation/node/recording/revision_uid": [
    {
      "_links": {
        "self": {
          "href": "https://www.example.org/user/37?_format=hal_json"
        },
        "type": {
          "href": "https://www.example.org/rest/type/user/user"
        }
      },
      "uuid": [
        {
          "value": "d40684cf-2321-42d2-9194"
        }
      ]
    }
  ],
  "https://www.example.org/rest/relation/node/recording/uid": [
    {
      "_links": {
        "self": {
          "href": "https://www.example.org/user/37?_format=hal_json"
        },
        "type": {
          "href": "https://www.example.org/rest/type/user/user"
        }
      },
      "uuid": [
        {
          "value": "d40684cf-2321-42d2-9194"
        }
      ],
      "lang": "en"
    }
  ],
  "https://www.example.org/rest/relation/node/recording/field_category": [
    {
      "_links": {
        "self": {
          "href": "https://www.example.org/simplecategory?_format=hal_json"
        },
        "type": {
          "href": "https://www.example.org/rest/type/taxonomy_term/tags"
        }
      },
      "uuid": [
        {
          "value": "3fef93d5-926a-41aa-95cb"
        }
      ]
    }
  ],
  "https://www.example.org/rest/relation/node/recording/field_part1_speaker": [
    {
      "_links": {
        "self": {
          "href": "https://www.example.org/by/speakername?_format=hal_json"
        },
        "type": {
          "href": "https://www.example.org/rest/type/taxonomy_term/author"
        }
      },
      "uuid": [
        {
          "value": "fb6c806f-fa6a-4aa0-89ef"
        }
      ]
    }
  ]
}

如何编写返回“ https://www.example.org/simplecategory?_format=hal_json”的查询?

然后我想要一个类似的查询,该查询返回“ https://www.example.org/by/speakername?_format=hal_json

因此jq '._embedded'为我提供了上面的数据。

然后,我尝试了各种组合以达到https://www.example.org/rest/relation/node/recording/field_category的值。
-jq '._embedded.https://www.example.org/rest/relation/node/recording/field_category-但网址中当然有特殊字符。

  • jq .["https://www.example.org/rest/relation/node/recording/field_category"]

  • jq ."https://www.example.org/rest/relation/node/recording/field_category$"

我还弄乱了一些内置函数的JQ,例如flatten和to_entries,from_entries。我也尝试过使用正则表达式,但是我的努力返回了Cannot iterate over null (null)

1 个答案:

答案 0 :(得分:1)

  

如何编写返回“ https://www.example.org/simplecategory?_format=hal_json”的查询?

如果您想显式指定顶级密钥,则后续查询将是:

.["https://www.example.org/rest/relation/node/recording/revision_uid"][]
| ._links.self.href

也就是说,整个查询将是:

._embedded
| .["https://www.example.org/rest/relation/node/recording/revision_uid"][]
| ._links.self.href
  

然后我想要一个类似的查询

明确指定顶级键的替代方法可能是从所有键的数组中选择感兴趣的href:

 ._embedded
 | [.[][]._links.self.href]

这将产生:

[
  "https://www.example.org/user/37?_format=hal_json",
  "https://www.example.org/user/37?_format=hal_json",
  "https://www.example.org/simplecategory?_format=hal_json",
  "https://www.example.org/by/speakername?_format=hal_json"
]