Forum Discussion

Nmenon81's avatar
Nmenon81
Occasional Contributor
7 years ago

Convert JSON response into an array - error

When I execute this code my json array returns [null, null].

What am I doign wrong?

//DB Response
def dbresponse = context.expand( '${JDBC Request#ResponseAsXml}' )
def dbsluper=new XmlSlurper().parseText(dbresponse)

//Json Response
def jsonresponse = context.expand( '${GetBenefitDetail_by Ben Seq#Response}' )
def jsluper=new JsonSlurper().parseText(jsonresponse)

//Writing the json specfic data into an array

def jsonvalues=[]
jsluper.GetBenefitDetailsResult.BenefitDetails.each{
 jsonvalues.add(it.key)
 jsonvalues.add(it.value)
}


jsonvalues.sort()
log.info jsonvalues

//Writing the DBData into array
def dbvalues= []
dbsluper.ResultSet.each{
 dbvalues.add(it.text())
}

log.info dbvalues

  • Lucian's avatar
    Lucian
    Community Hero

    I would introduce more logs in order to see what the problem is. For instance:

     

    //DB Response
    def dbresponse = context.expand( '${JDBC Request#ResponseAsXml}' )
    log.info dbresponse

    Can you try this and come back to us?

    • Nmenon81's avatar
      Nmenon81
      Occasional Contributor

      What I see is that adding the JDBC resultset into an array  is working right and the array is populated with values. .

      The JSON response after parsing (def jsluper=new JsonSlurper().parseText(jsonresponse)) is also returning values. But after I try to add the JSON values into an array and then I print the array to see what it contains - I get null, null for the JSON array.  I have attached my JDBC resultset and JSON response.

      NOthing has changed in my script

       

       

      • Radford's avatar
        Radford
        Super Contributor

        Please see the standalone example below using your JSON data, hopefully that will explain things.

         

        I think the issue is that what you are receiving from the JSON Slurper result is not what you expected. Note how I have used the getClass() method to tell what I have at the various stages.

         

        May I suggest that you read the JsonSlurper documentation and how and when it returns Maps and/or Lists.

         

        import groovy.json.JsonSlurper
        
        def jsonresponse = '''\
        {
           "GetBenefitDetailsResult" : {
              "BenefitDetails" : [
                 {
                    "CompanyCode" : "01",
                    "PolicyNumber" : "TEST",
                    "BenefitSeq" : 99,
                    "BenefitType" : "UV",
                    "ProcessedToDate" : 99999999,
                    "StatusCode" : "",
                    "StatusReason" : "",
                    "StatusDate" : 0,
                    "PlanCode" : "",
                    "IssueDate" : 0,
                    "ParentBenefitSeq" : 1,
                    "PayUpDate" : 0,
                    "MatureExpireDate" : 0,
                    "ProcessingCycle" : "",
                    "InfRiderSeq" : 0,
                    "LastChgDate" : 0,
                    "LastChgTime" : 538976288,
                    "DateOfBirth" : 0,
                    "IssueAge" : 0,
                    "UnderwritingClass" : "",
                    "SexCode" : "",
                    "InsuredDeathInd" : "",
                    "SecondInsuredDob" : 0,
                    "SecInsIssAge" : 0,
                    "SecInsUwcls" : "",
                    "SecInsSexCode" : "",
                    "SecInsDeathInd" : "",
                    "ValuePerUnit" : 0.00,
                    "NumberOfUnits" : 0.00000,
                    "AnnPremPerUnit" : 0.00000,
                    "ModePremium" : 0.00,
                    "CommMdlFctrsFl" : "",
                    "BenefitFee" : 0.00,
                    "NonCommHelthFlg" : "",
                    "CommBasedOnDate" : "",
                    "PremBasedOnDate" : "",
                    "UnitsNarFlag" : "",
                    "CategoryCode" : "",
                    "RenewalType" : "",
                    "AaModelFlag" : "",
                    "EtiMx" : 0E-7,
                    "PolLineOfBusnss" : "",
                    "ProductType" : "",
                    "AgentSplitCtl" : 0,
                    "CommissionClass" : "",
                    "IssueState" : "",
                    "ResidentState" : "",
                    "AuxAge" : 0,
                    "BasicBillDate" : 0,
                    "EffPremDate" : 0,
                    "MecDate" : 0,
                    "UnderwriterCode" : "",
                    "UnderwriterType" : "",
                    "ReinCode" : 0,
                    "PremTaxBasisFl" : "",
                    "JointFlag" : "",
                    "AllctdTargetPrem" : 0.00,
                    "AllctdMinPrem" : 0.00,
                    "TypeCode" : "",
                    "ParType" : "",
                    "NonForfeiture" : "",
                    "Dividend" : "",
                    "OtherInfo" : "",
                    "AccumDividends" : 0.0,
                    "PremiumsPaid" : 0.0,
                    "DividendsCredited" : 0.0,
                    "TaxBasis" : 0.0,
                    "ExcessDividend" : "",
                    "UlRiderFvSeq" : 0,
                    "CapOytOption" : "",
                    "MaximizeOytFlag" : "",
                    "OriginalUnits" : 0.0,
                    "OrLPOverride" : "",
                    "BillLoanNetDiv" : "",
                    "Opt8TarFlag" : "",
                    "EtiRpuEndowment" : 0.0,
                    "EtiRpuPattern" : "",
                    "PpPtOytDivs" : 0.0,
                    "FundValue" : {
                       "FvFundValueCode" : "",
                       "FvBasis1" : 0.0,
                       "FvBasis2" : 0.0,
                       "FvIncome1" : 0.00,
                       "FvIncome2" : 0.00,
                       "FvBalance1" : 0.00,
                       "FvBalance2" : 0.00,
                       "FvTotLoanBasis" : 0.00,
                       "FvTotalLoanInc" : 0.00,
                       "FvTotalLoanBal" : 0.00,
                       "FvGuarRate" : 0.00,
                       "FvGuarDeposits" : 0.00,
                       "FvDepLoads" : 0.00,
                       "FvGrWithdrawals" : 0.00,
                       "FvWithdrawalLoad" : 0.00,
                       "FvCoi" : 0.00,
                       "FvBaseDed" : 0.00,
                       "FvProcessLoads" : 0.00,
                       "FvProductType" : "",
                       "FvType" : "",
                       "FvProcessRule" : "",
                       "FvDepAlloc" : "",
                       "FvWtdAlloc" : "",
                       "FvDedAlloc" : "",
                       "FvCoiAlloc" : "",
                       "FvLonAlloc" : "",
                       "FvPldAlloc" : "",
                       "FvLastValDate" : 0,
                       "FvLandmarkDate" : 0,
                       "FvDepAllocFlag" : "",
                       "FvSpecialFund" : "",
                       "FvOrderNum" : "0",
                       "FvCommClass" : "",
                       "FvWaiverFee" : "",
                       "FvWaiverCalc" : "",
                       "FvFillerLock" : ""
                    },
                    "UnitValue" : {
                       "UvGuarCoiRate" : 1.28665,
                       "UvCurrCoiRate" : 0.82090,
                       "UvCurrentNar" : 90.26115,
                       "UvTotalBasisIss" : 0.00,
                       "UvTaxBasisIssue" : 0.00,
                       "UvEi1" : 0.00,
                       "UvEi2" : 0.00,
                       "UvEi3" : 0.00,
                       "UvEi4" : 0.00,
                       "UvEi5" : 0.00,
                       "UvEi6" : 0.00,
                       "UvEi7" : 0.00,
                       "UvEi8" : 0.00,
                       "UvEi9" : 0.00,
                       "UvEi10" : 0.00,
                       "UvEi11" : 0.00,
                       "UvEi12" : 0.00,
                       "UvAnnEi1" : 0.00,
                       "UvAnnEi2" : 0.00,
                       "UvAnnEi3" : 0.00,
                       "UvAnnEi4" : 0.00,
                       "UvAnnEi5" : 0.00,
                       "UvAnnEi6" : 0.00,
                       "UvAnnEi7" : 0.00,
                       "UvAnnEi8" : 0.00,
                       "UvAnnEi9" : 0.00,
                       "UvAnnEi10" : 0.00,
                       "UvAnnEi11" : 0.00,
                       "UvAnnEi12" : 0.00,
                       "UvTotSpecialAmt" : 100000.00,
                       "Uv10201988SpAmt" : 0.00,
                       "Uv12311986FnVal" : 0.00,
                       "Uv12311988FnVal" : 0.00,
                       "UvWaiverFee" : "",
                       "UvWaiverCalc" : "",
                       "UvFillerLock" : ""
                    },
                    "OtherRider" : {
                       "OrRiderType" : "",
                       "OrMthlyDed" : 0.0,
                       "OrPuaFace" : 0.0
                    },
                    "Base" : {
                       "BaThirdPrtyFlag" : "",
                       "BaOrTotInfFlag" : "",
                       "BaTamraPrem" : 0.0,
                       "BaTamraFlag" : "",
                       "BaOrDivIntegra" : "",
                       "BaOrVanishFlag" : "",
                       "BaOrPuaSeq" : 0.0,
                       "BaOrSpecialFee" : "",
                       "BaOrWaiverFee" : "",
                       "BaOrWaiverCalc" : "",
                       "BaOrBenefitLock" : 0.0,
                       "BaOrTamraTrmnt" : "",
                       "BaOrCovLvlDiv" : ""
                    },
                    "ShadowBenefit" : {
                       "SuTypeCode" : "",
                       "SuSubtypeCd" : "",
                       "SuUnitsCtrl" : "",
                       "SuDeduction" : 0.0,
                       "SuPremiumsPaid" : 0.0,
                       "SuDivCredited" : 0.0,
                       "SuTaxBasis" : 0.0,
                       "SuCompSubType" : "",
                       "SuTotInfFlag" : "",
                       "SuLPOverride" : "",
                       "SuTamraFlag" : "",
                       "SuRowSource" : "",
                       "SuBundledBenSeq" : 0,
                       "SuJointSelection" : "",
                       "SuSpecialFee" : "",
                       "SuWaiverFee" : "",
                       "SuWaiverCalc" : "",
                       "SuBenefitLock" : 0,
                       "SuTamraTrmnt" : ""
                    },
                    "SurrenderLoad" : {
                       "SlTableCode" : "",
                       "SlRateUpAge" : 0,
                       "SlPercent" : 0.0,
                       "SlFlatAmount" : 0.0,
                       "SlPctCeaseDate" : 0,
                       "SlFlatCeaseDate" : 0,
                       "Sl2NdTableCode" : "",
                       "Sl2NdRateAgeUp" : 0,
                       "Sl2NdPercent" : 0.0,
                       "Sl2NdFlatAmount" : 0.0,
                       "Sl2NdPctCsDate" : 0,
                       "Sl2NdFltCsDate" : 0,
                       "SlPctMthlyDed" : 0.0,
                       "SlFlatMthlyDed" : 0.0,
                       "SlPremiumsPaid" : 0.0,
                       "SlDivCredited" : 0.0,
                       "SlTaxBasis" : 0.0,
                       "SlLPOverride" : "",
                       "SlWaiverCalc" : "",
                       "SlBenefitLock" : 0
                    },
                    "PaidUp" : {
                       "PuTypeCode" : "",
                       "PuIntPdToDate" : 0,
                       "PuAccruInt" : 0.0,
                       "PuAccruFaceAmt" : 0.0,
                       "PuPremiumsPaid" : 0.0,
                       "PuDivCredited" : 0.0,
                       "PuTaxBasis" : 0.0,
                       "PuSpecialFee" : "",
                       "PuWaiverFee" : "",
                       "PuWaiverCalc" : "",
                       "PuBenefitLock" : 0
                    },
                    "AnnuityRider" : {
                       "ArBillingMode" : "",
                       "ArBillingForm" : "",
                       "ArBilledToDate" : 0,
                       "ArPaidToDate" : 0,
                       "ArActBillDate" : 0,
                       "ArBillableAmount" : 0.00,
                       "ArSurrenderLoad" : 0.0,
                       "ArCurrAccumCash" : 0.0,
                       "ArLastValDate" : 0,
                       "ArOther" : "",
                       "ArVarCovrFlag" : "",
                       "ArWithProcDate" : 0,
                       "ArFvCreatnFlag" : "",
                       "ArPremDepRque" : "",
                       "ArIndexedCovrFl" : "",
                       "ArWaiverFee" : "",
                       "ArWaiverCalc" : "",
                       "ArBenefitLock" : 0
                    },
                    "BaseFund" : {
                       "BfNonForfeiture" : "",
                       "BfSpecifiedAmt" : 0.0,
                       "BfCurrentDb" : 0.0,
                       "BfDbOption" : "",
                       "BfCurrSurrLoad" : 0.0,
                       "BfMinimumPremium" : 0.0,
                       "BfTarget" : 0.0,
                       "BfTamraAmount" : 0.0,
                       "BfGuidelinLvlPr" : 0.0,
                       "BfGuidelinSngPr" : 0.0,
                       "BfLastValDate" : 0,
                       "BfDateNegative" : 0,
                       "BfThirdPrtyFlag" : "",
                       "BfTotInfFlag" : "",
                       "BfVarCovrFlag" : "",
                       "BfWithProcDate" : 0,
                       "BfFvCreatnFlag" : "",
                       "BfPremDepRque" : "",
                       "BfRothIraCbFlg" : "",
                       "BfBaseTargetExp" : 0.0,
                       "BfWaiverFee" : "",
                       "BfWaiverCalc" : "",
                       "BfBenefitLock" : 0,
                       "BfIndexedCovrFl" : "",
                       "BfTreasryCvrFlg" : "",
                       "BfHybridCovrFlg" : ""
                    },
                    "SpecifiedAmountIncrease" : {
                       "SpOriginFlag" : "",
                       "SpSpecifiedAmt" : 0.0,
                       "SpTotInfFlag" : ""
                    }
                 }
              ],
              "GUID" : "TEST51564564DFSSFG",
              "ReturnCode" : 0,
              "Message" : null
           }
        }'''
        
        def jsluper=new JsonSlurper().parseText(jsonresponse)
        
        def jsonvalues=[]
        
        log.info(jsluper.GetBenefitDetailsResult.BenefitDetails.getClass())
        
        // BenefitDetails is a list, albeit with only one item.
        jsluper.GetBenefitDetailsResult.BenefitDetails.each{ benefitDetail ->
        
          // Each List item is a Map
          log.info(benefitDetail.getClass())
          log.info(benefitDetail.toString())
        
          // You then need to loop through each map entry
          benefitDetail.each(){ mapEntry -> 
          	
          	log.info(mapEntry.getClass())
             log.info(mapEntry.getKey())
        	log.info(mapEntry.getValue())
        	
             // Note: The map entry value may not be a simple string value.
             log.info(mapEntry.getValue().getClass())
             
             jsonvalues.add(mapEntry.getKey())
             jsonvalues.add(mapEntry.getValue())
          }
        }
        
        log.info(jsonvalues)