Home > OS >  How to retrieve all key-value pairs avoiding key duplication from JSON in Groovy script
How to retrieve all key-value pairs avoiding key duplication from JSON in Groovy script

Time:04-13

I am totally new to groovy script and would like some help to solve this out. I have a JSON response I want to manipulate and get desired parameters back by avoiding duplication. The Json response does not have indexes like 0,1,2.. that I can iterate through.

Here is the response that I want to work with:

{
"AuthenticateV2" : {
  "displayName" : "Verification of authentication",
  "description" : "notification ",
  "smsTemplate" : "authentication.v2.0_sms",
  "emailHeaderTemplate" : "v2.0_header",
  "emailBodyTemplate" : "html",
  "parameters" : {
     "displayName" : "USER_DISPLAY_NAME",
     "actionTokenURL" : "VERIFICATION_LINK",
     "customToken" : "VERIFICATION_CODE"
  },
  "supportedPlans" : [
     "connectGo"
  ]
},
"PasswordRecovery" : {
  "displayName" : "Verification of password recovery",
  "description" : "notification",
  "smsTemplate" : "recovery.v1.0_sms",
  "emailHeaderTemplate" : "recovery.v1.0_header",
  "emailBodyTemplate" : "recovery.v1.0_body_html",
  "parameters" : {
     "displayName" : "USER_DISPLAY_NAME",
     "actionTokenURL" : "VERIFICATION_LINK",
     "customToken" : "VERIFICATION_CODE",
     "adminInitiated" : false,
     "authnId" : "AUTHENTICATION_IDENTIFIER",
     "authnType" : "EMAIL",
     "user" : {
        "displayName" : "USER_DISPLAY_NAME"
     }
  },
  "supportedPlans" : [
     "connectGo"
  ]
},
"PasswordReset" : {
  "displayName" : "password reset",
  "description" : "notification",
  "smsTemplate" : "recovery.v1.0_sms",
  "emailHeaderTemplate" : "recovery.v1.0_header",
  "emailBodyTemplate" : "html",
  "parameters" : {
     "displayName" : "USER_DISPLAY_NAME",
     "user" : {
        "displayName" : "USER_DISPLAY_NAME"
     }
  }

The expected output that I want to have:

{
     "displayName" : "USER_DISPLAY_NAME",
     "actionTokenURL" : "VERIFICATION_LINK",
     "customToken" : "VERIFICATION_CODE",
     "customToken" : "VERIFICATION_CODE",
     "adminInitiated" : false,
     "authnId" : "AUTHENTICATION_IDENTIFIER",
     "authnType" : "EMAIL"
  }

I need to retrieve all fields under parameters tag and also want to avoid duplication

CodePudding user response:

You should first get familiar with parsing and producing JSON in Groovy.

Then, assuming the provided response is a valid JSON (it's not - there are 2 closing curlies (}) missing at the end) to get all the parameters keys merged into one JSON we have to convert the JSON string into a Map object first using JsonSlurper:

def validJsonResponse = '<your valid JSON string>'
Map parsedResponse = new JsonSlurper().parseText(validJsonResponse) as Map

Now, when we have a parsedResponse map we can iterate over all the root items in the response and transform them into the desired form (which is all the unique parameters keys) using Map::collectEntries method:

Map uniqueParameters = parsedResponse.collectEntries { it.value['parameters'] }

Finally, we can convert the uniqueParameters result back into a pretty printed JSON string using JsonOuput:

println JsonOutput.prettyPrint(JsonOutput.toJson(uniqueParameters))

After applying all the above we'll get the output

{
    "displayName": "USER_DISPLAY_NAME",
    "actionTokenURL": "VERIFICATION_LINK",
    "customToken": "VERIFICATION_CODE",
    "adminInitiated": false,
    "authnId": "AUTHENTICATION_IDENTIFIER",
    "authnType": "EMAIL",
    "user": {
        "displayName": "USER_DISPLAY_NAME"
    }
}

If you want to get rid of user entry from the final output just remove it from the resulting uniqueParameters map (uniqueParameters.remove('user')) before converting it back to JSON string.

  • Related