# How to get desired data from JSON

**URL:** <https://forum.ansible.com/t/how-to-get-desired-data-from-json/36467>\
**Category:** Ansible Project\
**Created:** [March 22, 2022, 5:01am UTC](https://forum.ansible.com/t/how-to-get-desired-data-from-json/36467 "2022-03-22T05:01:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Know\_Your\_Tech](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Know\_Your\_Tech](https://forum.ansible.com/u/Know_Your_Tech)\
**Post date:** [March 22, 2022, 5:01am UTC](https://forum.ansible.com/t/how-to-get-desired-data-from-json/36467/1 "2022-03-22T05:01:02Z")

</div>

I need to print the below for any `jdbc_data_source` found

expected output:

jdbc\_data\_source name is has username and jndi name \<jndi\_name\>

which will print as

jdbc\_data\_source name is `cwds` has username `CAN_USER` and jndi name `cwdsjndi`

Here is my JSON:

[  
{  
“?xml”: {  
“attributes”: {  
“encoding”: “UTF\_8”,  
“version”: “1.0”  
}  
}  
},  
{  
“jdbc\_data\_source”: [  
{  
“attributes”: {  
“xmlns”: “[http://xmlns.oracle.com/weblogic/jdbc\_data\_source](http://xmlns.oracle.com/weblogic/jdbc_data_source)”  
}  
},  
{  
“name”: “cwdsjndi”  
},  
{  
“jdbc\_driver\_params”: [  
{  
“url”: “jdbc:oracle:thin:@[//myhost.myshop.com:1521/OLTT206](https://myhost.myshop.com:1521/OLTT206)”  
},  
{  
“driver\_name”: “oracle.jdbc.OracleDriver”  
},  
{  
“properties”: {  
“property”: [  
{  
“name”: “user7”  
},  
{  
“value”: “CAN\_USER”  
}  
]  
}  
},  
{  
“password\_encrypted”: “{AES}BcqmURyYoCkLvC5MmREXsfpRMO93KPIubqUAbb95+nE=”  
}  
]  
},  
{  
“jdbc\_data\_source\_params”: {  
“jndi\_name”: “cwds”  
}  
}  
]  
},  
{  
“?xml”: {  
“attributes”: {  
“encoding”: “UTF\_8”,  
“version”: “1.0”  
}  
}  
},  
{  
“jdbc\_data\_source”: [  
{  
“attributes”: {  
“xmlns”: “[http://xmlns.oracle.com/weblogic/jdbc\_data\_source](http://xmlns.oracle.com/weblogic/jdbc_data_source)”  
}  
},  
{  
“name”: “dsvelcw”  
},  
{  
“jdbc\_driver\_params”: [  
{  
“url”: “jdbc:oracle:thin:@myhost.myshop.com:1521:DB01”  
},  
{  
“driver\_name”: “oracle.jdbc.OracleDriver”  
},  
{  
“properties”: {  
“property”: [  
{  
“name”: “user”  
},  
{  
“value”: “WEB\_USER”  
}  
]  
}  
},  
{  
“password\_encrypted”: “{AES}WYImLDLo0j7S80cGMUDsE5M5QTnpffTPGyIzDuGG6WU=”  
}  
]  
},  
{  
“jdbc\_data\_source\_params”: {  
“jndi\_name”: “dsvelcw”  
}  
}  
]  
}  
]

All I could achieve so far is to read the json and loop over it as below:

---

<div class="post-metadata">

**Author:** ![Dennis\_McCarthy1](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_McCarthy1](https://forum.ansible.com/u/Dennis_McCarthy1)\
**Post date:** [March 22, 2022, 5:14pm UTC](https://forum.ansible.com/t/how-to-get-desired-data-from-json/36467/2 "2022-03-22T17:14:27Z")

</div>

JSON isn’t my strong point but I can pull out the individual elements and you wanted to create the msg. Hopefully this will help you to complete what you’re trying to do.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [localhost] =\> {  
“msg”: “ **jdbc\_data\_source name is cwds has username CAN\_USER and jndi name cwdsjndi** ”  
}

This is the playbook:

---

<div class="post-metadata">

**Author:** ![Dennis\_McCarthy1](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_McCarthy1](https://forum.ansible.com/u/Dennis_McCarthy1)\
**Post date:** [March 25, 2022, 7:29am UTC](https://forum.ansible.com/t/how-to-get-desired-data-from-json/36467/3 "2022-03-25T07:29:36Z")

</div>

I added a GitHub repo with a few examples showing how to parse your JSON file.

I’ve already had a contribution extending my code and creating a loop to pull out all the matches.

[https://github.com/dmccuk/json\_parsing\_with\_ansible](https://github.com/dmccuk/json_parsing_with_ansible)
