JQ 选择值包含字符串且值不为空的对象



尝试从 JSON 数组中选择值包含字符串且值不为空的对象。

期望输出:

    {
      "configurable": false,
      "property_reference": ".properties.blobstore_certificate",
      "property_type": "rsa_cert_credentials",
      "product_guid": "p-blah-29d4678e926cf2069871",
      "location": "ops_manager",
      "variable_path": "something",
      "issuer": "/C=US/O=Blah",
      "valid_from": "2019-01-16T19:55:11Z",
      "valid_until": "2021-01-16T19:55:11Z"
    }

选择以下选项时:

{
  "certificates": [
    {
      "configurable": false,
      "property_reference": ".properties.director_ssl",
      "property_type": "rsa_cert_credentials",
      "product_guid": "p-blah-29d4678e926cf2069871",
      "location": "ops_manager",
      "variable_path": null,
      "issuer": "/C=US/O=Blah",
      "valid_from": "2019-01-16T19:55:10Z",
      "valid_until": "2021-01-16T19:55:10Z"
    },
    {
      "configurable": false,
      "property_reference": ".properties.uaa_ssl",
      "property_type": "rsa_cert_credentials",
      "product_guid": "p-blah-29d4678e926cf2069871",
      "location": "ops_manager",
      "variable_path": null,
      "issuer": "/C=US/O=Blah",
      "valid_from": "2019-01-16T19:55:10Z",
      "valid_until": "2021-01-16T19:55:10Z"
    },
    {
      "configurable": false,
      "property_reference": ".properties.blobstore_certificate",
      "property_type": "rsa_cert_credentials",
      "product_guid": "p-blah-29d4678e926cf2069871",
      "location": "ops_manager",
      "variable_path": "something",
      "issuer": "/C=US/O=Blah",
      "valid_from": "2019-01-16T19:55:11Z",
      "valid_until": "2021-01-16T19:55:11Z"
    }
  ]
}

用:

curl blah | jq ' .certificates[] | select(.variable_path|test("thing"))'

获取:

jq: error (at <stdin>:0): null (null) cannot be matched, as it is not a string

我认为问题是存在空值。我不确定如何在存在这些空值的情况下进行选择而不会收到错误。

从考虑中删除null值的一种方法是:

.certificates[] | select(.variable_path | strings | test("thing"))

您也可以考虑使用 ? ,可以像这样在这里使用:

.certificates[] | select(.variable_path | test("thing")?)

相关内容

  • 没有找到相关文章

最新更新