Skip to content
\n

Is there any simpler solution?

","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"

Nushell's type checking does require a little bit more verboseness here, but there are some alternative forms you can try.

\n

In all versions, I think =~ (or the new like operator) are probably better options than str contains, but that's just preference.

\n
# Sample data for demo purposes\nlet sample_json = [\n  {\n    \"id\": 1,\n    \"optional_field\": \"123value\"\n  },\n  {\n    \"id\": 2,\n    \"optional_field\": \"456\"\n  },\n  {\n    \"id\": 3\n  }\n]
\n

Options:

\n

Option 1: try block to discard error results

\n

The most concise, and beats out your original version by a few characters. Just wraps the str contains (or =~) in a try to ignore errors:

\n
$sample_json | where {try {$in.optional_field =~ value}}\n# or\n$sample_json | where {try {$in.optional_field | str contains value}}\n# or\n$sample_json | where {try {$in.optional_field like value}}
\n

Option 2: Double-filter

\n

Same effect as the and version below. This is my preference, as I think it's the most \"readable\" of the four options:

\n
$sample_json\n| where optional_field? != null\n| where optional_field =~ value\n
\n

As above, you can substitute str contains or like based on your preference.

\n

Option 3: An and condition

\n

Perhaps more verbose, but possibly a bit more clear than the original version, at least. I'd definitely use (1) or (2) above personally, but I know some people prefer the single where/and compared to a double-where.

\n
$sample_json | where optional_field? != null and optional_field =~ value\n# Alternatively, multi-line:\n$sample_json | where (\n  $it.optional_field? != null\n) and (\n  $it.optional_field =~ value\n)
\n

and short-circuits the operation for null fields and never runs the =~ check.

","upvoteCount":1,"url":"https://github.com/nushell/nushell/discussions/14216#discussioncomment-11112034"}}}

Playing with Record's optional key in a List #14216

Answered by NotTheDr01ds
akhansari asked this question in Q&A
Discussion options

You must be logged in to vote

Nushell's type checking does require a little bit more verboseness here, but there are some alternative forms you can try.

In all versions, I think =~ (or the new like operator) are probably better options than str contains, but that's just preference.

# Sample data for demo purposes
let sample_json = [
  {
    "id": 1,
    "optional_field": "123value"
  },
  {
    "id": 2,
    "optional_field": "456"
  },
  {
    "id": 3
  }
]

Options:

Option 1: try block to discard error results

The most concise, and beats out your original version by a few characters. Just wraps the str contains (or =~) in a try to ignore errors:

$sample_json | where {try {$in.optional_field =~ value}}
# or
$sample_json |

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@akhansari
Comment options

@NotTheDr01ds
Comment options

@akhansari
Comment options

Answer selected by akhansari
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants