简体   繁体   中英

How to parse JSON structure to get the string for the last element in groovy script

I am trying to parse the below JSON in groovy script to read the last value of the parameter element below at the end of the JSON message

I tried: def error = payload.childInstances.parameter But it returns a null value.

I am expecting that it returns me the last instance of parameter "com.sap.esb.oauth.token.access.TokenAccessException: OAuth2 Authorization Code Credential 'OAUTH_MSOFFICE65_CRED' not found"

Payload looks like this
{
   "message":{
      "subsystemName":"CONTENT",
      "subsytemPartName":"CONTENT_DEPLOY",
      "messageId":"ComponentMonitorErrors",
      "messageText":"Error messages reported by component monitors"
   },
   "childInstances":[
      {
         "message":{
            "subsystemName":"CAMEL",
            "subsytemPartName":"IFLOW",
            "messageId":"ERROR",
            "messageText":"Integration flow failed."
         },
         "childInstances":[
            {
               "message":{
                  "subsystemName":"CAMEL",
                  "subsytemPartName":"IFLOW",
                  "messageId":"EXCEPTION",
                  "messageText":"{0}"
               },
               "parameter":[
                  "org.osgi.service.blueprint.container.ComponentDefinitionException: Error when instantiating bean MessageFlow_62_configurator of class null"
               ],
               "childInstances":[
                  {
                     "message":{
                        "subsystemName":"CAMEL",
                        "subsytemPartName":"IFLOW",
                        "messageId":"CAUSE",
                        "messageText":"Cause: {0}"
                     },
                     "parameter":[
                        "com.sap.esb.oauth.token.access.TokenAccessException: Problem during reading the OAuth2 Authorization Code Credential OAUTH_MSOFFICE65_CRED from the cache: com.sap.esb.oauth.token.access.TokenAccessException: OAuth2 Authorization Code Credential \u0027OAUTH_MSOFFICE65_CRED\u0027 not found"
                     ],
                     "childInstances":[
                        {
                           "message":{
                              "subsystemName":"CAMEL",
                              "subsytemPartName":"IFLOW",
                              "messageId":"CAUSE",
                              "messageText":"Cause: {0}"
                           },
                           "parameter":[
                              "com.google.common.util.concurrent.UncheckedExecutionException: com.sap.esb.oauth.token.access.TokenAccessException: OAuth2 Authorization Code Credential \u0027OAUTH_MSOFFICE65_CRED\u0027 not found"
                           ],
                           "childInstances":[
                              {
                                 "message":{
                                    "subsystemName":"CAMEL",
                                    "subsytemPartName":"IFLOW",
                                    "messageId":"CAUSE",
                                    "messageText":"Cause: {0}"
                                 },
                                 "parameter":[
                                    "com.sap.esb.oauth.token.access.TokenAccessException: OAuth2 Authorization Code Credential \u0027OAUTH_MSOFFICE65_CRED\u0027 not found"
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}
 

you have recursive structure

code could be like this:

def c = payload.childInstances
//find last nested child instance
while( c[-1].childInstances ) c = c[-1].childInstances
def p = c[-1].parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM