Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!