Forum Discussion

swprabhu's avatar
swprabhu
Occasional Contributor
6 years ago

How to Store Json node values in an array

Say the two response names are REST response and BLS response. I want to store their node values in an array and compare them. How do I do it 

 

The main structure of REST response is something like this

Security 1

Taxlist 1
Term =Short // I want to store this as RESTmatrix[0]
Taxlist 2
Term =Long // I want to store this as RESTmatrix[1]

Security 2
Taxlist 1
Term= Short // I want to store this as RESTmatrix[2]
Taxlist 1
Term= Short // I want to store this as RESTmatrix[3]
Taxlist 1
Term= Short // I want to store this as RESTmatrix[4]

Security ...


The structure of BLS response is like belw

Security 1 TaxList 1 Term // I want to store this as Bls[0]
Security 1 Taxlist 2 Term // I want to store this as Bls[1]
Security 2 Taxlist 1 Term // I want to store this as Bls[2]

And then compare RESTmatrix[0] to Bls[0]
RESTmatrix[1] to Bls[1] and so on

 

So I want to read the term of all taxlists and store them in an array say RESTmatrix

The same thing I would like to do for BLS response and store it an array of list (say BLS matrix)

And then I want to compare and see if they match


The code I written so far can read the different term values for each security but I want to store them in a sequential array


//Reading RESTresponse
def RESTresponse = context.expand( '${REST_001#ResponseAsXml#declare namespace ns1=\'https://dev-svcgateway.betasys.com/v1/TaxLotGainLoss/GetRealizedByAccount\'; //ns1:Response[1]}' )
def slurpRest = new XmlSlurper().parseText(RESTresponse)


//Reading the number of securities in REST response

def seccount =slurpRest.getRealizedByAccountResponse.securityList.e.size()
log.info ("Number of securities="+seccount)


//Reading the term for each security taxlist

for (b=0;b<seccount;b++)
{
def secname =slurpRest.getRealizedByAccountResponse.securityList.e[b].securityNumber
log.info secname
def taxlotcount =slurpRest.getRealizedByAccountResponse.securityList.e[b].taxLotList.e.size()
log.info("Taxlotcount="+ taxlotcount)

for (c=0;c<taxlotcount;c++)
{
def trys=slurpRest.getRealizedByAccountResponse.securityList.e[b].taxLotList.e[c].gainLossForTaxLot.term.text().toUpperCase()
log.info ("term value" +trys) // This I want to store in an array with an index

}


}

 

3 Replies

  • New2API's avatar
    New2API
    Frequent Contributor

    swprabhu, can you please post actual json  (only for 2 or 3 securities) response with some dummy data. 

     

    security1 can have mutiple taxlots. so I am not sure how they are stored under security1.

     

    Thanks.

    • richie's avatar
      richie
      Community Hero

      Hi,

       

      what was the solution you used?  Can you provide a bit more info as the answer would be valuable to other people.

       

      thanks,

       

      richie