Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
How to use jq try with multiple conditions like a case?

I need to output in a single column the value of a field A if it's not null or the value of field B if not null or nothing if both A and B are null.

When I just had filed A to test I wrote this which worked ok

try .fieldA catch ""

But now as I need to take field B if A is null I tried those, and nothing worked

try .fieldA catch try .fieldB catch "" => this only returned empty values ever
try (.fieldA or .fieldB) catch ""  => this one outputs true or false, 2 resultst instead of 1
try (.fieldA,.fieldB) catch ""  => this one outputs both A and B if both are not nulll, so 2 resultst instead of 1

I'd like the try to stop evaluating whenever the first result is not null

Thanks

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use the alternate operator //, which takes the second alternative if the first is null or false, and empty for the "nothing" result. If accessing any field might fail, additionally use the optional operator ? on those fields.

{
  "fieldA": "A1 present",
  "fieldB": "B1 present",
  "fieldC": "irrelevant"
}
{
  "fieldB": "B2 present",
  "fieldC": "irrelevant"
}
{
  "fieldA": "A3 present",
  "fieldC": "irrelevant"
}
{
  "fieldC": "irrelevant"
}
jq '.fieldA // .fieldB // empty'
"A1 present"
"B2 present"
"A3 present"

Demo


Addressing @peak's "warning": If you want to capture a filed that has the explicit boolean value false, while not capturing it if it is either missing or explicitly set to null, you can use values which selects non-null values only, and first to retrieve the first one that exists among a given stream of alternatives:

{
  "fieldA": "A1 present",
  "fieldB": "B1 present",
  "fieldC": "irrelevant"
}
{
  "fieldA": false,
  "fieldB": "B2 present",
  "fieldC": "irrelevant"
}
{
  "fieldA": null,
  "fieldB": "B3 present",
  "fieldC": "irrelevant"
}
{
  "fieldB": "B4 present",
  "fieldC": "irrelevant"
}
{
  "fieldC": "irrelevant"
}
jq 'first(.fieldA, .fieldB | values)'
"A1 present"
false
"B3 present"
"B4 present"

Demo

Using this approach, there's no need to provide the explicit empty case. However, if you want to have a default fallback, add it as the last item in the stream, e.g. first(.fieldA, .fieldB, "" | values).

over 4 years ago · Santiago Trujillo Relatório

0

jq's // operator has quite complicated semantics (*) and in particular, when evaluating E // F, no distinction is made between E being null, false, or the empty stream.

Also, given an object as input, the expression .x will evaluate to the JSON value null if the key "x" is explicitly specified as null or if the key "x" is not present at all.

Thus, assuming an object has been given as input, if we want .fieldA if .fieldA is non-null, and otherwise .fieldB if .fieldB is non-null, and otherwise the string "missing", then we would have to write something along the lines of:

  if   .fieldA != null then .fieldA
  elif .fieldB != null then .fieldB
  else "missing"
  end

Simply replace "missing" by empty to achieve the objective stated in the original question.


(*) See the jq FAQ for complete details.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda