# Filter the output with jmespath

**URL:** <https://forum.ansible.com/t/filter-the-output-with-jmespath/893>\
**Category:** Get Help\
**Tags:** windows, jmespath, playbook\
**Created:** [September 18, 2023, 3:01pm UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893 "2023-09-18T15:01:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daemenseth](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/daemenseth/32/677_2.png) [@daemenseth](https://forum.ansible.com/u/daemenseth)\
**Post date:** [September 18, 2023, 3:01pm UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893/1 "2023-09-18T15:01:45Z")

</div>

I have a playbook which install windows updates with win\_updates.  
The output is registred. See example output below.  
I want to filter that output with the result title,kb where the installed value is equal to true

I was trying something like this:

> updates.\*.[?installed==“true”]{name:title,installed:installed}  
> But that is not working.

> {  
> “updates”: {  
> “adab657d-7124-4d67-98a0-78bb2d9db5bf”: {  
> “id”: “adab657d-7124-4d67-98a0-78bb2d9db5bf”,  
> “title”: “2023-09 Cumulative Update for .NET Framework 3.5, 4.8 and 4.8.1 for Microsoft server operating system version 21H2 for x64 (KB5030186)”,  
> “categories”: [  
> “Microsoft Server operating system-21H2”,  
> “Security Updates”  
> ],  
> “kb”: [  
> “5030186”  
> ],  
> “installed”: true  
> },  
> “91722aa2-d45a-4a79-b59a-2365f590b8c6”: {  
> “id”: “91722aa2-d45a-4a79-b59a-2365f590b8c6”,  
> “title”: “Windows Malicious Software Removal Tool x64 - v5.117 (KB890830)”,  
> “categories”: [  
> “Update Rollups”,  
> “Windows Server 2016”,  
> “Windows Server 2019”,  
> “Windows Server, version 1903 and later”  
> ],  
> “kb”: [  
> “890830”  
> ],  
> “installed”: true  
> },  
> “f1e3c23a-45c2-47ae-a9ca-7139585571d5”: {  
> “id”: “f1e3c23a-45c2-47ae-a9ca-7139585571d5”,  
> “title”: “2023-09 Cumulative Update for Microsoft server operating system version 21H2 for x64-based Systems (KB5030216)”,  
> “categories”: [  
> “Security Updates”  
> ],  
> “kb”: [  
> “5030216”  
> ],  
> “installed”: true  
> },  
> “696baba6-f559-4e24-b099-3170606e6096”: {  
> “id”: “696baba6-f559-4e24-b099-3170606e6096”,  
> “title”: “Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.397.1155.0)”,  
> “categories”: [  
> “Definition Updates”,  
> “Microsoft Defender Antivirus”  
> ],  
> “kb”: [  
> “2267602”  
> ],  
> “installed”: true  
> }  
> },  
> “failed\_update\_count”: 0,  
> “found\_update\_count”: 4,  
> “changed”: true,  
> “reboot\_required”: true,  
> “installed\_update\_count”: 4,  
> “filtered\_updates”: {},  
> “\_ansible\_no\_log”: false,  
> “failed\_when\_result”: false  
> }

---

<div class="post-metadata">

**Author:** ![IPvSean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ipvsean/32/1153_2.png) [@IPvSean](https://forum.ansible.com/u/IPvSean)\
**Post date:** [September 18, 2023, 3:39pm UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893/2 "2023-09-18T15:39:09Z")

</div>

> [@daemenseth](#):
>
> I want to filter that output with the result title,kb where the installed value is equal to true

Can you just use the `selectattr` filter and do `selectattr('installed', 'equalto', true)` and then get what you want out of that data structure?

To your original question maybe something like this-\>

```auto
    - name: Filter and display updates
      debug:
        msg: "{{ your_json_variable | json_query(jmespath_query) }}"
      vars:
        jmespath_query: "result.updates.* | [?installed == `true`] | [].[title, kb]"

```

---

<div class="post-metadata">

**Author:** ![daemenseth](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/daemenseth/32/677_2.png) [@daemenseth](https://forum.ansible.com/u/daemenseth)\
**Post date:** [September 18, 2023, 8:09pm UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893/3 "2023-09-18T20:09:44Z")

</div>

> [@IPvSean](#):
>
> `[?installed == `true`]`

thnx @IPvSean  
that jmespath filter is working.

I try later this week also your suggestion about the selectattr

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 19, 2023, 8:11am UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893/4 "2023-09-19T08:11:53Z")

</div>

The [JMESPath Terminal](https://github.com/jmespath-community/jmespath.terminal) makes working out expressions easier, I cleaned up your JSON (removing smart quotes):

```json
{
  "updates": {
    "adab657d-7124-4d67-98a0-78bb2d9db5bf": {
      "id": "adab657d-7124-4d67-98a0-78bb2d9db5bf",
      "title": "2023-09 Cumulative Update for .NET Framework 3.5",
      "4.8 and 4.8.1 for Microsoft server operating system version 21H2 for x64 (KB5030186)": "",
      "categories": [
        "Microsoft Server operating system-21H2",
        "Security Updates"
      ],
      "kb": [
        "5030186"
      ],
      "installed": true
    },
    "91722aa2-d45a-4a79-b59a-2365f590b8c6": {
      "id": "91722aa2-d45a-4a79-b59a-2365f590b8c6",
      "title": "Windows Malicious Software Removal Tool x64 - v5.117 (KB890830)",
      "categories": [
        "Update Rollups",
        "Windows Server 2016",
        "Windows Server 2019",
        "Windows Server, version 1903 and later"
      ],
      "kb": [
        "890830"
      ],
      "installed": true
    },
    "f1e3c23a-45c2-47ae-a9ca-7139585571d5": {
      "id": "f1e3c23a-45c2-47ae-a9ca-7139585571d5",
      "title": "2023-09 Cumulative Update for Microsoft server operating system version 21H2 for x64-based Systems (KB5030216)",
      "categories": [
        "Security Updates"
      ],
      "kb": [
        "5030216"
      ],
      "installed": true
    },
    "696baba6-f559-4e24-b099-3170606e6096": {
      "id": "696baba6-f559-4e24-b099-3170606e6096",
      "title": "Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.397.1155.0)",
      "categories": [
        "Definition Updates",
        "Microsoft Defender Antivirus"
      ],
      "kb": [
        "2267602"
      ],
      "installed": true
    }
  },
  "failed_update_count": 0,
  "found_update_count": 4,
  "changed": true,
  "reboot_required": true,
  "installed_update_count": 4,
  "filtered_updates": {},
  "_ansible_no_log": false,
  "failed_when_result": false
}

```

Saved it as a file locally and then opened it using JMESPath Terminal and this expression:

```auto
updates.*|[?installed==`true`].{name:title,kb:kb[0]}

```

Returns:

```json
[
  {
    "name": "2023-09 Cumulative Update for .NET Framework 3.5",
    "kb": "5030186"
  },
  {
    "name": "Windows Malicious Software Removal Tool x64 - v5.117 (KB890830)",
    "kb": "890830"
  },
  {
    "name": "2023-09 Cumulative Update for Microsoft server operating system version 21H2 for x64-based Systems (KB5030216)",
    "kb": "5030216"
  },
  {
    "name": "Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.397.1155.0)",
    "kb": "2267602"
  }
]

```

It took me a while to realise that the back ticks @IPvSean used above are essential — booleans and [numbers](https://github.com/jmespath/jmespath.js/issues/85) have to use backticks.

---

<div class="post-metadata">

**Author:** ![gwmngilfen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gwmngilfen/32/6_2.png) [@gwmngilfen](https://forum.ansible.com/u/gwmngilfen)\
**Post date:** [October 6, 2023, 9:08am UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893/5 "2023-10-06T09:08:00Z")

</div>

Hi @daemenseth! It looks like the post might be solved - could you check to see if the response by @chris worked for you?

If so, it would be _super_ helpful if you could click the ✔ on their post to accept the solution - it helps users find solutions (solved topics have a higher search priority), recognises the input of the people who help you, helps our volunteers find new issues to answer, and keeps the forum nice and tidy. It’s just a nice way to give back, and only takes a moment 🙂

Thanks!  
(this is template reply, do feel free to reply if I’ve misunderstood the situation!)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 5, 2023, 9:08am UTC](https://forum.ansible.com/t/filter-the-output-with-jmespath/893/6 "2023-11-05T09:08:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
