# How to search through a JSON output for a specific key:value?

**URL:** <https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772>\
**Category:** Ansible Project\
**Created:** [September 9, 2020, 6:59am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772 "2020-09-09T06:59:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Short](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albert\_Short](https://forum.ansible.com/u/Albert_Short)\
**Post date:** [September 9, 2020, 6:59am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/1 "2020-09-09T06:59:24Z")

</div>

Morning all,

have had this question from a co-worker & not managed to work out how do do it. Any help is greatly appreciated as I can see this coming up several times in the future as well.

We have the following response body

{

“products”: [

{

“chartName”: “action-orchestrator”,

“installTime”: 1599482114,

“modifiedTime”: 1599482114,

“name”: “Action Orchestrator”,

“revision”: 1,

“status”: {

“message”: “Product is ready”,

“progressPercentage”: 100,

“state”: “running”

},

“version”: [

“5.2.0”

]

},

{

“chartName”: “cloudcenter-shared”,

“installTime”: 1599483017,

“modifiedTime”: 1599483017,

“name”: “cloudcenter-shared”,

“revision”: 1,

“status”: {

“message”: “Product is ready”,

“progressPercentage”: 100,

“state”: “running”

},

“version”: [

“5.4.0”

]

},

{

“chartName”: “common-framework”,

“installTime”: 1599469174,

“modifiedTime”: 1599469174,

“name”: “Suite Admin”,

“revision”: 1,

“status”: {

“message”: “Product is ready”,

“progressPercentage”: 100,

“state”: “running”

},

“version”: [

“5.2.0”

]

},

{

“chartName”: “workload-manager”,

“installTime”: 1599483064,

“modifiedTime”: 1599483064,

“name”: “Workload Manager”,

“revision”: 1,

“status”: {

“message”: “Waiting for product to be ready”,

“progressPercentage”: 67,

“state”: “creating”

},

“version”: [

“5.4.0”

]

}

],

“status”: {

“message”: “Product install success”,

“progressPercentage”: 100,

“state”: “running”

}

}

What we need to do is search through ‘products’, & check that the ‘status.state’ is ‘running’ for a specific ‘name’, in our case ‘Workload Manager’

I can’t work out how to provide the search, with the expected result for the co-wokrer to use with an ‘until’ module.

The idea is to only execute the next task, when the ‘status.state’ changes to ‘running’.

Hope this makes sense!

Regards

---

<div class="post-metadata">

**Author:** ![Tom\_Black](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Tom\_Black](https://forum.ansible.com/u/Tom_Black)\
**Post date:** [September 9, 2020, 7:14am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/2 "2020-09-09T07:14:02Z")

</div>

HI

from programming view, you should decode json string to a data structure (hash), then access to the hash.

my test:

$ cat a.json

{“a”:123,“b”:456,“c”:789}

$ perl -MJSON -le ‘open FD,“a.json”;$str=;$hash=JSON-\>new-\>decode($str);print $hash-\>{c}’

789

Thanks.

---

<div class="post-metadata">

**Author:** ![Albert\_Short](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albert\_Short](https://forum.ansible.com/u/Albert_Short)\
**Post date:** [September 9, 2020, 7:24am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/3 "2020-09-09T07:24:54Z")

</div>

Tom,

thanks for that reply, but it is way beyond my current experience.

but looking at your reply & what i am looking for it doesn’t seem to match.

Under ‘products’ you can see there are several “groups” using the same keys but potentially different values, with nothing unique that i can use to show the difference between them, other the the values for each key. I want to understand if I can ‘sort’ through the response body, find the required ‘name’ & associate the correct ‘status.state’ to it.

Your example only shows one ‘group’ with clear associations, my response body has multiple similar keys but different values.

Thanks for the response though, but I can’t see how it would help me.

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [September 9, 2020, 7:38am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/4 "2020-09-09T07:38:16Z")

</div>

You can read about

- jmespath - [https://jmespath.org/](https://jmespath.org/)
- Use jsonpath - [https://jsonpath.com](https://jsonpath.com), you may end up something like this

$.products[?(@.chartName == ‘workload-manager’)]

You can take this free course - [https://kodekloud.com/courses/enrolled/635226](https://kodekloud.com/courses/enrolled/635226) to know more about JSON Path.

---

<div class="post-metadata">

**Author:** ![Albert\_Short](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albert\_Short](https://forum.ansible.com/u/Albert_Short)\
**Post date:** [September 9, 2020, 7:45am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/5 "2020-09-09T07:45:06Z")

</div>

Abhijeet,

thanks for that, it looks interesting, but I would like to try to see if there is a solution with as minimal extra libraries needed. Trying to keep it within the Ansible sphere & use Ansible/Jinja2 filters or regex as much as possible.  
It may not be possible, I don’t know, but the end customer wants to keep the coding within Ansible as much as possible & not be reliant on addition packages. Not an easy task but I do need to find out.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 9, 2020, 8:10am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/6 "2020-09-09T08:10:03Z")

</div>

> Abhijeet,
> 
> thanks for that, it looks interesting, but I would like to try to see if there is a solution with as minimal extra  
> libraries needed. Trying to keep it within the Ansible sphere & use Ansible/Jinja2 filters or regex as much as possible.  
> It may not be possible, I don't know, but the end customer wants to keep the coding within Ansible as much as possible &  
> not be reliant on addition packages. Not an easy task but I do need to find out.

You can use the json\_query filter, which uses the jmespath query language.

I'm sure you can find examples with the search engine of your choice.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [September 9, 2020, 8:19am UTC](https://forum.ansible.com/t/how-to-search-through-a-json-output-for-a-specific-key-value/33772/7 "2020-09-09T08:19:26Z")

</div>


