0

I have the following large JSON I receive from a specific REST call. I want to only extract the password strings (highlighted in bold). So I need the values. ConfigPWD_USER.password, ConfigPWD_ROOT.password, instanceOwnerPassword, databaseUserPassword, fencedUserPassword in java. Guess I will have to search using string regex. Can someone help?

{
    "deployment_name": "name1",
    "model": {
        "model": {
            "description": "",
            "nodes": [{
                "id": "OS Node",
                "ptype": "image:OS Node",
                "attributes": {
                    <more json key value pairs>
                    **"ConfigPWD_USER.password": "<xor>NzozMzA=",
                    "ConfigPWD_ROOT.password": "<xor>NzozMzA="**
                },
                "type": "image:OS,
                "groups": {},
                "locked": []
            }, {
                "id": "disk for db instance",
                "attributes": {
                    <more json key value pairs>
                },
                "type": "add disk:1.0.0",
                "groups": {},
                "locked": []
            }, {
                "id": "disk for database data",
                "attributes": {
                    <more json key value pair>
                },
                "type": "disk:1.0.0",
                "groups": {},
                "locked": []
            }, {
                "id": "DB Server",
                "startsAfter": ["disk for db instance",
                    "OS Node"
                ],
                "locked": ["instanceMountPoint", "instanceMountPoint", "instanceMountPoint"],
                "attributes": {
                    "instanceMountPoint": "${disk for db instance.MOUNT_POINT}",
                    <more json key value pair>
                    **"instanceOwnerPassword": "<xor>NzozMzA=",**
                    <more json key value pair>
                    **"fencedUserPassword": "<xor>NzozMzA=",**

                },
                "type": "DBLUN",
                "groups": {}
            }, {
                "id": "bludbs",
                "startsAfter": ["DB Server", "disk for database data"],
                "locked": ["instanceName", "dataMountPoint", "instanceName", "dataMountPoint", "instanceName", "dataMountPoint"],
                "attributes": {
                    <many json key value pairs>,
                    **"databaseUserPassword": "<xor>NzozMzA="**,
                    <many json key value pairs>
                },s
                "type": "sript:scrip1.exe",
                "groups": {}
            }],
            "app_type": "application",
            "name": "Default DB in Linux",
            "patterntype": "vsys",
            "links": [{
                    "id": "HostedOnLink_1",
                    "source": "disk for db instance",
                    "target": "OS Node",
                    "attributes": {},
                    "type": "HostedOnLink",
                    "groups": {}
                },
                {
                    "id": "HostedOnLink_2",
                    "source": "disk for database data",
                    "target": "OS Node",
                    "attributes": {},
                    "type": "HostedOnLink",
                    "groups": {}
                }, {
                    "id": "HostedOnLink_3",
                    "source": "DB Server",
                    "target": "OS Node",
                    "attributes": {},
                    "type": "HostedOnLink",
                    "groups": {}
                }, {
                    "id": "HostedOnLink_4",
                    "source": "bludb",
                    "target": "OS Node",
                    "attributes": {},
                    "type": "HostedOnLink",
                    "groups": {}
                }
            ],
            "locked": false,
            "readonly": false,
            "version": "1.0",
            "patternversion": "1.2.3.0",
            "mixinArgs": null
        },
        "layers": [{
            "id": "layer",
            "nodes": ["OS Node", "disk for db instance", "disk for database data", "DB Server", "bludb"]
        }]
    },
    <more json key value pairs>,
    "organizations": []
}
Ali Azim
  • 160
  • 1
  • 15
Vasanth Raghavan
  • 162
  • 2
  • 12
  • 2
    why you are not using JSON processing libraries? The most common JSON processing libraries in Java are: Jackson, Gson, json-io, Genson – Ali Azim Mar 27 '19 at 12:22
  • thanks @AliAzim for your immediate comment. Unfortunately I am not sure I can include those libraries in my project. So looking for string search solutions. – Vasanth Raghavan Mar 27 '19 at 12:28
  • 1
    And in groovy there is in-build groovy.json.JsonSlurper - no additional libs required – daggett Mar 27 '19 at 12:49
  • thanks @daggett yes looks like I can use JsonSlurper. So what is the idea here ? owing to the multi level json tree, what do u suggest ? ideally i can convert json to a list but that may not work here right ? – Vasanth Raghavan Mar 27 '19 at 12:57
  • Have a look at https://stackoverflow.com/a/23655354/708998 – dawogfather Mar 27 '19 at 13:06

1 Answers1

1

in groovy there is in-build groovy.json.JsonSlurper and code to find one of the values could be like this:

def json = new groovy.json.JsonSlurper().parseText('''
{
    "deployment_name": "name1",
    "model": {
        "model": {
            "description": "",
            "nodes": [{
                "id": "OS Node",
                "ptype": "image:OS Node",
                "attributes": {
                    "ConfigPWD_USER.password": "1<xor>NzozMzA=",
                    "ConfigPWD_ROOT.password": "1<xor>NzozMzA="
                },
                "type": "image:OS",
                "groups": {},
                "locked": []
            }, {
                "id": "disk for db instance",
                "attributes": {
                    "ConfigPWD_USER.password": "2<xor>NzozMzA=",
                    "ConfigPWD_ROOT.password": "2<xor>NzozMzA="
                },
                "type": "add disk:1.0.0",
                "groups": {},
                "locked": []
            }]
        }
    }    
}            
''')

json.model.model.nodes.collect{n-> n.attributes."ConfigPWD_USER.password" }
daggett
  • 26,404
  • 3
  • 40
  • 56
  • thanks @daggett the above solution worked. I owe an explanation here. With standalone code i was able to get the code to work. However I need to add the jsonSlurper related libs to my production code and I think I may not be able to do so without acquiring more approvals. Hence I had created the other post : https://stackoverflow.com/questions/55404075/java-string-extract-a-specific-pattern However I realize now this is not acceptable. – Vasanth Raghavan Mar 29 '19 at 06:27
  • is it jenkins? you don't need any library for jenkins slurper... it's easier to get approval then write regexp. or don't mark your question with regexp as `java/groovy` tags... just regexp maybe somebody will help you. btw there is a site: https://regex101.com/ that helps to write regexp – daggett Mar 29 '19 at 11:32