How to get desired data from JSON

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
}
},
{
“name”: “cwdsjndi”
},
{
“jdbc_driver_params”: [
{
“url”: “jdbc:oracle:thin:@//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
}
},
{
“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:

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:

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