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

157
Visualizações
Combining multiple tests in validating JSON content with jq

I have following JSON object

{"color": "red", "shapes": [ "square", "triangle" ]}

I would like to validate the JSON object using jq using following conditions:

  • color has value "red"
  • shapes does not contain value "round"

The returned result should be either true or false.

I have 2 jq command which validate both conditions, but I'm not sure how to combine this into 1 expression:

json='{"color": "red", "shapes": [ "square", "triangle" ]}'
echo "$json" | jq '.["color"] | test("red")'
echo "$json" | jq 'any(.shapes[]; contains("round"))|not'

Any pointers or help would be appreciated.

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

0

You can simply verify that both tests return true with all:

echo '{"color": "red", "shapes": [ "square", "triangle" ]}' |
  jq '[(.["color"] | test("red")),
       (any(.shapes[]; contains("round"))|not)
      ] | all'

Create an array containing the results of each test, then pipe that array to all.

over 4 years ago · Santiago Trujillo Relatório

0

A correct way to test a collection of conditions is using and.

In your case, a correct test would be:

(.color == "red") and (.shapes|index("round") == null)

Example (typescript):

jq '(.color == "red") and (.shapes|index("round") == null)'
{"color": "red", "shapes": [ "square", "triangle" ]}
true

In jq, not is a syntactically ordinary filter, so you could write the second condition as: (.shapes | index("round") | not).

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