Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle JMESPath contains filter on attribute that may be null?

I want to filter the output from the Azure CloudShell command az ad sp list which outputs a JSON array, eg by filtering to Publisher Name = "ACME". All az commands support a --query argument which accepts a JMESPath expression.

I have a JMESPath filter:

az ad sp list --query "[?contains(publisherName,'ACME')]" --all

that fails with error:

In function contains(), invalid type for value: None, expected one of: ['array', 'string'], received: "null"

I'm confident of my JMESPath syntax since a very similar expression works correctly:

az ad sp list --query "[?contains(displayName,'ACME')]" --all

I have a null filter that works fine:

az ad sp list --query "[?publisherName!='null']" --all

But when I combine the null filter with the contains filter I still get the error:

az ad sp list --query "[?publisherName!='null' && contains(publisherName,'ACME')]" --all

I'm guessing that JMESPath filter doesn't support boolean operations short circuit. However I didn't find any statement about that on http://jmespath.org/ or by googling.

I don't know how to daisy chain or pipe with the Azure az command --query clause to apply the two filters separately.

Any suggestion how to achieve my filtered output?

like image 976
JohnC Avatar asked Mar 22 '19 15:03

JohnC


1 Answers

The not_null function comes handy. It keeps the intent of the query more readable.

az ad sp list --query "[?contains(not_null(publisherName,''),'ACME')]" --all

The function returns the first non-null argument. This is often called "coalesce" elsewhere.

like image 194
Palec Avatar answered Sep 28 '22 15:09

Palec