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.
\nIn 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\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]
Options:
\ntry
block to discard error resultsThe 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}}\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}}
Same effect as the and
version below. This is my preference, as I think it's the most \"readable\" of the four options:
$sample_json\n| where optional_field? != null\n| where optional_field =~ value\n
As above, you can substitute str contains
or like
based on your preference.
and
conditionPerhaps 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
.
$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)
and
short-circuits the operation for null
fields and never runs the =~
check.
-
I have a json containing a list of objects. When I do So I'm forced to be very verbose like this: open my.json | where ($it.optional_field? | default "" | str contains value) Is there any simpler solution? |
Beta Was this translation helpful? Give feedback.
-
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 # Sample data for demo purposes
let sample_json = [
{
"id": 1,
"optional_field": "123value"
},
{
"id": 2,
"optional_field": "456"
},
{
"id": 3
}
] Options: Option 1:
|
Beta Was this translation helpful? Give feedback.
-
another not yet mentioned option is to just fill up all the non-existent fields beforehand: open my.json | default "" optional_field | where optional_field =~ value |
Beta Was this translation helpful? Give feedback.
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 newlike
operator) are probably better options thanstr contains
, but that's just preference.Options:
Option 1:
try
block to discard error resultsThe most concise, and beats out your original version by a few characters. Just wraps the
str contains
(or=~
) in atry
to ignore errors: