2

I have a .json file that I would like to filter through jq to return the entire object in the array if the key value in that object equals 'failed'. How would I do this ?

Example json:

"version": "0.26.0",
"controls": [{
    "id": "os-1.0",
    "status": "passed",
    "code_desc": "File /etc/profile content should match /umask\\s*022/",
    "profile_id": "test"
}, {
    "id": "os-1.0",
    "status": "passed",
    "code_desc": "File /etc/bashrc content should match /umask\\s*022/",
    "profile_id": "test"
}, {
    "id": "os-1.0",
    "status": "failed",
    "code_desc": "File /etc/csh.cshrc content should match /umask\\s*022/",
    "profile_id": "test"
   "message": "\nexpected: \"/sbin/sulogin\"\n     got: \n\n(compared using `cmp` matcher)\n"
}]

Output to a new file :

{
    "id": "os-1.0",
    "status": "failed",
    "code_desc": "File /etc/csh.cshrc content should match /umask\\s*022/",
    "profile_id": "test"
    "message": "\nexpected: \"/sbin/sulogin\"\n     got: \n\n(compared using  `cmp` matcher)\n"
}
2
  • can you please try github.com/stedolan/jq/wiki/… Commented Jul 13, 2016 at 16:33
  • @BhavinSolanki This is what I've tried so far : cat file.json | jq -c '.[] | select(.controls | . and contains("failure"))' but I believe I'm missing an array coordinate Commented Jul 13, 2016 at 16:38

1 Answer 1

4

The text shown as JSON is invalid as JSON. Assuming it is fixed to have the structure {"version": _, "controls": _}, the following filter would yield the result shown below:

.controls[] | select(.status == "failed")

Output:

{
  "id": "os-1.0",
  "status": "failed",
  "code_desc": "File /etc/csh.cshrc content should match /umask\\s*022/",
  "profile_id": "test",
  "message": "\nexpected: \"/sbin/sulogin\"\n     got: \n\n(compared using `cmp` matcher)\n"
}

Note: For robustness you might want to use .status? instead of .status.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.