Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda