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

Rad*_*ord 1 null select json 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"
    }
Run Code Online (Sandbox Code Playgroud)

选择时:

{
  "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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

使用:

curl blah | jq ' .certificates[] | select(.variable_path|test("thing"))'
Run Code Online (Sandbox Code Playgroud)

获得:

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

我认为问题在于存在空值。我不确定如何在不出现错误的情况下选择这些空值。

pea*_*eak 6

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

.certificates[] | select(.variable_path | strings | test("thing"))
Run Code Online (Sandbox Code Playgroud)

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

.certificates[] | select(.variable_path | test("thing")?)
Run Code Online (Sandbox Code Playgroud)