Legacy Dev Forum Posts

 View Only

Sign Up

Cycle on array in json response with template

  • 1.  Cycle on array in json response with template

    Posted 06-05-2025 18:43

    Luca_Assandro | 2020-11-18 15:07:10 UTC | #1

    Hello PureCloud Developers

    Can I ask for a guidance to remove nested arrays in data action's response to read it in contract?

    Hera the response json: { "userProfileHB": { "ndglist": [ { "ndgId": "137640", "ndgCode": "18645", "accountList": [ { "accountCode": "12345", "accountAddress": { "zipcode: "50031", "country: "Italy" } }, { "accountCode": "67890", "accountAddress": { "zipcode: "20021", "country: "Italy" } } ] }, { "ndgId": "55555", "ndgCode": "44444", "accountList": [ { "accountCode": "67890", "accountAddress": { "zipcode: "20021", "country: "Italy" } } ] } ] } }

    the problem is "accountList" array, that is nested in "ndglist". I want to write a response template to cycle "accountList" array to create an "accounts" single object like this:

    { "userProfileHB": { "ndglist": [ { "ndgId": "137640", "ndgCode": "18645", "accounts": { "accountNumber": 2, "accountCode": "12345|67890", "accountAddress": {

    "zipcode: "50031

    20021",

    "country: "Italy

    Italy"

    } } }, { "ndgId": "55555", "ndgCode": "44444", "accounts": { "accountNumber": 1, "accountCode": "67890", "accountAddress": { "zipcode: "20021", "country: "Italy" } } } ] } }

    I searched but I didn't find any information about foreach or loop cycle, any suggestion?

    Thanks Luca


    Jason_Mathison | 2020-11-18 16:44:49 UTC | #2

    Hi Luca,

    Is there a standard number of accounts in the account list, or a limit to the number? If so, it might be possible to do some magic with the JSONPath in the translation map.

    The JSON wasn't valid so I ended up having to add some quotes. I also changed the last account code to make it unique to make it more obvious if my JsonPath queries were working correctly. Here is what I ended up with: (Note that I highlighted my JSON and clicked on the " (blockquote) button to keep the formatting of it)

    { "userProfileHB": { "ndglist": [{ "ndgId": "137640", "ndgCode": "18645", "accountList": [{ "accountCode": "12345", "accountAddress": { "zipcode": "50031 ", "country": "Italy" } }, { "accountCode": "67890", "accountAddress": { "zipcode": "20021 ", "country": "Italy " } } ] }, { "ndgId": "55555", "ndgCode": "44444", "accountList": [{ "accountCode": "44555", "accountAddress": { "zipcode": "20021 ", "country": "Italy " } }] } ] } }

    Putting this into my favorite online JsonPath evaluator: http://jsonpath.herokuapp.com/ You can do things like pull out the account codes for the first result's accountList: $.userProfileHB.ndglist[0].accountList.*.accountCode which returns [ "12345", "67890"]

    If you only have to support a limited number of ndglist objects you should be able to build up a response contract and success response that you can process in architect. If the number of items in an array is needed you can use Velocity markup to get that value with something like $list.size($accountCodes)

    Another option is to run this request via a lambda to get it into an easy to process shape you want before returning it to the data action. A few lines of Javascript can do a lot.

    --Jason


    Luca_Assandro | 2020-11-19 07:57:03 UTC | #3

    Thank you so much Jason, I really appreciate your help!


    Luca_Assandro | 2020-11-19 09:53:01 UTC | #4

    @Jason_Mathison , one more question: how is possible to normalize null value? During array's reading I have an error if one element is null instead ofvalorized, how can i valorize it with blank value?

    Thanks, Luca


    Jason_Mathison | 2020-11-19 10:21:52 UTC | #5

    Can you provide an example of your data action configuration and the response you are getting back from the endpoint?


    Luca_Assandro | 2020-11-19 10:52:14 UTC | #6

    The problem is on more field, alla the array with null value (without " char), we can consider "listaNdg_branchCode" string field.

    This is the output contract: { "title": "risposta", "type": "object", "properties": { "esito": { "type": "string" }, "bankCust": { "type": "string" }, "birthDate": { "type": "string" }, "graz": { "type": "string" }, "recurrenceTime": { "type": "string" }, "customerStart": { "type": "string" }, "invoice": { "type": "string" }, "ndgHB": { "type": "string" }, "accountHB": { "type": "string" }, "accountChargeHB": { "type": "string" }, "flagTol": { "type": "integer" }, "customerCode": { "type": "string" }, "profileId": { "type": "string" }, "customerName": { "type": "string" }, "taxCode": { "type": "string" }, "currencyChargeCode": { "type": "string" }, "referenceEmail": { "type": "string" }, "referenceTelephoneNumber1": { "type": "string" }, "abiCode": { "type": "string" }, "cabCode": { "type": "string" }, "branchCode": { "type": "string" }, "customerSurname": { "type": "string" }, "grazDesc": { "type": "string" }, "management": { "type": "string" }, "descContr": { "type": "string" }, "userRolecodeRole": { "type": "string" }, "userRolebank": { "type": "string" }, "userProductlistaServiziserviceBankLimitvalue": { "type": "array", "items": { "title": "userProductlistaServiziserviceBankLimitvalue", "type": "integer" } }, "userProductlistaServiziserviceCode": { "type": "array", "items": { "title": "userProductlistaServiziserviceCode", "type": "string" } }, "userProductlistaServiziserviceBankLimitdesc": { "type": "array", "items": { "title": "userProductlistaServiziserviceBankLimitdesc", "type": "string" } }, "userProductlistaServiziserviceDescription": { "type": "array", "items": { "title": "userProductlistaServiziserviceDescription", "type": "string" } }, "userProductlistaServiziserviceBankLimitcurrency": { "type": "array", "items": { "title": "userProductlistaServiziserviceBankLimitcurrency", "type": "string" } }, "userProductlistaServiziprofileLimitlimitId": { "type": "array", "items": { "title": "userProductlistaServiziprofileLimitlimitId", "type": "string" } }, "userProductlistaServiziserviceBankLimitbank": { "type": "array", "items": { "title": "userProductlistaServiziserviceBankLimitbank", "type": "string" } }, "userProductlistaServiziprofileLimituserId": { "type": "array", "items": { "title": "userProductlistaServiziprofileLimituserId", "type": "string" } }, "userProductlistaServizithresholdSms": { "type": "array", "items": { "title": "userProductlistaServizithresholdSms", "type": "integer" } }, "listaNdgndgId": { "type": "array", "items": { "title": "listaNdgndgId", "type": "string" } }, "listaNdgndgCode": { "type": "array", "items": { "title": "listaNdgndgCode", "type": "string" } }, "listaNdgndgType": { "type": "array", "items": { "title": "listaNdgndgType", "type": "string" } }, "listaNdgbankCode": { "type": "array", "items": { "title": "listaNdgbankCode", "type": "string" } }, "listaNdgndgDesc": { "type": "array", "items": { "title": "listaNdgndgDesc", "type": "string" } }, "listaNdgndgTypeDesc": { "type": "array", "items": { "title": "listaNdgndgTypeDesc", "type": "string" } }, "listaNdgndgCorrelationCode": { "type": "array", "items": { "title": "listaNdgndgCorrelationCode", "type": "string" } }, "listaNdgbranchCode": { "type": "array", "items": { "title": "listaNdgbranchCode", "default": "vuoto", "type": "string" } } }, "additionalProperties": true }

    Here the response after output trasformation: { "esito": "OK", "flagTol": 2, "customerCode": "92691631", "profileId": "128085", "customerName": "JAQXO", "taxCode": "sssssssssssssss", "currencyChargeCode": "EUR", "referenceEmail": "xxxxx", "referenceTelephoneNumber1": "yyyyyyy", "abiCode": "05387", "cabCode": "12900", "branchCode": "0000", "customerSurname": "ZILOQQABA", "grazDesc": "Listino Standard", "userRolecodeRole": "PF000001", "userRolebank": "05387", "userProductlistaServiziserviceBankLimitdesc": [ "INFO", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "INFO", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "INFO", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "INFO", "INFO", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "DISPO UNLIMITED", "INFO", "INFO" ], "userProductlistaServiziserviceBankLimitcurrency": [ "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR", "EUR" ], "userProductlistaServiziserviceBankLimitvalue": [ 0, 0, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 0, 999999999999, 0, 999999999999, 0, 999999999999, 999999999999, 999999999999, 999999999999, 0, 0, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 0, 999999999999, 0, 0, 999999999999, 999999999999, 999999999999, 999999999999, 0, 999999999999, 0, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 0, 0, 999999999999, 999999999999, 0, 999999999999, 0, 999999999999, 999999999999, 0, 0, 0, 999999999999, 999999999999, 999999999999, 0, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 0, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 999999999999, 0, 999999999999, 999999999999, 999999999999, 999999999999, 0, 0, 0, 999999999999, 999999999999, 999999999999, 999999999999, 0, 0 ], "userProductlistaServiziserviceBankLimitbank": [ "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387", "05387" ], "userProductlistaServiziserviceBankLimitbLCode": [ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ], "userProductlistaServiziprofileLimitlimitId": [ "287563", "287517", "418131", "287523", "287569", "287556", "287541", "287553", "287538", "287551", "287543", "287498", "287521", "287516", "287561", "287557", "287481", "287550", "287520", "32", "366994", "287535", "287476", "287562", "287494", "287495", "287478", "287515", "287542", "287522", "287540", "287484", "287539", "287555", "287530", "287506", "287571", "287559", "287546", "287528", "287503", "287560", "373723", "287568", "287537", "287533", "287469", "287507", "287531", "287565", "287472", "287490", "287544", "287500", "287513", "418133", "287567", "287573", "287536", "287548", "287508", "287572", "287491", "287471", "287519", "287489", "287564", "287510", "287529", "287470", "287554", "287482", "287545", "287509", "287518", "287511", "287496", "287558", "287483", "287549", "287505", "365733", "287492", "287514", "287552", "287532", "287525", "287526", "287547", "287566", "287486", "287487", "287499" ], "userProductlistaServiziprofileLimituserId": [ "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631", "92691631" ], "userProductlistaServizithresholdSms": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "userProductlistaServiziserviceCode": [ "021", "048", "001", "511", "536", "507", "508", "049", "501", "032", "114", "037", "502", "036", "009", "503", "025", "027", "506", "003", "012", "026", "008", "010", "028", "029", "018", "034", "510", "509", "505", "038", "504", "006", "030", "107", "232", "233", "234", "015", "039", "235", "062", "051", "019", "052", "022", "221", "045", "035", "054", "220", "223", "304", "005", "122", "044", "230", "013", "020", "225", "229", "236", "302", "303", "055", "024", "231", "016", "031", "053", "222", "224", "227", "301", "002", "033", "226", "228", "023", "059", "060", "007", "014", "040", "047", "004", "011", "017", "042", "041", "043", "046" ], "userProductlistaServiziserviceDescription": [ "Condizioni carte", "Documenti", "Albero di navigazione", "Demo", "Home page portale", "Dati Personali", "News della Banca", "Help", "Autenticazione", "Personalizzazione Menu", "Condizioni Finanziamenti", "Comunicazioni cliente banca", "Servizio Clienti", "Home page portale", "Cornice", "Furti e Smarrimenti", "Movimenti carte", "Offerta Carte", "Grafici", "Bonifico singolo", "Pagamento MAV-RAV-Freccia", "Ricarica carta prepagata", "Bonifico verso i miei conti", "Il mio profilo", "Acquisto Carte", "Customer Feedback Management", "Posizione carte", "Blocco carte", "Guida", "Selezione banca del gruppo", "Dashboards", "Il mio mutuo", "Ricerca Agenzia", "Condizioni", "Domiciliazione Utenze", "Bonifico Estero", "Alert", "Fondi", "Paniere", "Rubrica beneficiari", "Il mio prestito", "Operazioni Societarie", "Bonifico Istantaneo", "Money transfer", "Servizi SMS carte", "Offerta finanziamenti", "Ricarica telefonica", "Portafoglio", "Richiesta mutuo", "Coordinate bancarie", "Riepilogo pagamenti", "Mercati", "Azioni, Warrant, Diritti, Obb. Conv", "Riepilogo e Quietanza", "Lista movimenti", "Amazon Ricarica", "Richiesta certificazione interessi", "Notizie", "Pagamento Ri.Ba", "Servizi SMS conto", "ETF", "Valute", "Contenuti Evolutivi", "Stato Delega", "Revoca Delega", "I miei investimenti", "Trasferimento disponibilita su conto", "Portafoglio Virtuale", "Bilancio Personale", "Richiesta Assegni circolari", "Offerta previdenza e protezione", "Ordini e Movimenti", "Obbligazioni e Titoli di Stato", "Preferiti", "Pagamento Delega", "Bonifico periodico", "Donazioni", "C. Warrant e Certificati", "Indici", "Situazione assegni", "Dipiu Service", "Bollettino CBill", "Bollettino postale", "Revoca Bonifico", "Offerta conti", "Sicurezza", "Posizione conto", "Pagamento F24", "Accredito stipendio pensione", "Pagamento bollo auto", "Pagamento canone RAI", "Richiesta Assicurazione Arca", "Richiesta prestito" ], "listaNdgndgId": [ "137640", "137544", "137675" ], "listaNdgndgCode": [ "18645", "260307", "2409543" ], "listaNdgndgType": [ "INT", "INT", "DEL" ], "listaNdgbankCode": [ "05387", "05387", "05387" ], "listaNdgndgDesc": [ "ZILOQQABA JAQXO", "WLUBYAIKA LIZOLJU", "YIQIPZABA ULAUBBU" ], "listaNdgndgTypeDesc": [ "Intestatario", null, "Delegato" ], "listaNdgndgCorrelationCode": [ "null", "null", null ], "listaNdgbranchCode": [ null, null, null ] }

    This is the error in "validate output against schema" { "message": "JSON failed schema validation for the following reasons: Schema: # @/properties/listaNdgbranchCode/items. Error location: /listaNdgbranchCode/0. instance type (null) does not match any allowed primitive type (allowed: [\"string\"]),\nSchema: # @/properties/listaNdgbranchCode/items. Error location: /listaNdgbranchCode/1. instance type (null) does not match any allowed primitive type (allowed: [\"string\"]),\nSchema: # @/properties/listaNdgbranchCode/items. Error location: /listaNdgbranchCode/2. instance type (null) does not match any allowed primitive type (allowed: [\"string\"])", "code": "invalid.schema", "status": 400, "messageParams": {}, "contextId": "500059d8-ec8b-4da6-9203-2a51608e3421", "details": [ { "errorCode": "ACTION.PROCESSING" } ], "errors": [] }

    thanks Luca


    Jason_Mathison | 2020-11-19 11:36:13 UTC | #7

    Hi Luca,

    I can get the data action to pass test mode by making this type of change to the last three parts of your output contract:

    "listaNdgbranchCode": { "type": "array", "additionalItems": true, "items": { "title": "listaNdgbranchCode", "default": "vuoto", "type": "string" } }

    to

    "listaNdgbranchCode": { "type": "array", "additionalItems": true, "items": { "title": "listaNdgbranchCode", "default": "vuoto", "type": ["string", "null"] } }

    This allows the arrays to contain null. Architect appears to be ok with this configuration, considering this to be a collection of strings. You will need to use the Architect "IsSet" function to verify that a value in this collection has a string value (not null) prior to using it, or else you risk your flow crashing and going to the error handler.

    Let me know how it goes.

    --Jason


    Luca_Assandro | 2020-11-19 14:18:23 UTC | #8

    Thank you Jason, one more time! An info: have you some reference to search all this options?

    Luca


    Jason_Mathison | 2020-11-19 14:46:56 UTC | #9

    If you are asking about the change I suggested like this: "type": ["string", "null"] that is standard JSON schema: https://json-schema.org/understanding-json-schema/reference/type.html

    If that is not what you are asking about can you ask your question a different way?

    --Jason


    Luca_Assandro | 2020-11-19 15:02:18 UTC | #10

    Yes, I was asking for "type": ["string", "null"]

    Thanks Luca


    system | 2020-12-20 15:02:20 UTC | #11

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 9341