Return integer or string instead of None from a JMESPath query


Is there a way to return an integer or a string instead of None?
I know that I can do an additional check like:

item = {"SX": {"BX": 1}}

value ="SX.BX", item) if"SX.BX", item) else 0

but the condition is very long and I would like to make it easier.

Asked By: alek vertysh



You can build that logic in your JMESPath query:

SX.BX || `0`

Given the empty JSON:


Would yield you 0, as you are excepting it.

So, you Python code becomes:

value ="SX.BX || `0`", item)
Answered By: β.εηοιτ.βε
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.