Best way to populate JSON query in Deluge - exemple of JSON STRING MAP to create contract with api

Best way to populate JSON query in Deluge - exemple of JSON STRING MAP to create contract with api


Hi every one just want to share about process with you.. I just want to share my code to build JSON string in DELUGE.

As the api is not as complete as javascript.. we need to have tricks..  For myself i have 1rst try to use the combination of Map(); and List(); with toString(); function to build the perfect JSON Map but it was realy a long script, difficult to read.. so now i just concatenate string or part of string...

Its important to create json string instead of pure json as square Bracket List "[0,1,2,..]" is not supported and will be squish into curly brace "{}" on save.

Exemple :  THIS 
  1. {
  2.   "metaApiName": "contract-end-date",
  3.   "inputs": [
  4.     {
  5.       "inputApiName": "contract-end-date",
  6.       "inputValue": 3
  7.     },
  8.     {
  9.       "inputApiName": "n-monthsyears-value",
  10.       "inputValue": "contract_month"
  11.     },
  12.     {
  13.       "inputApiName": "n-monthsyears-term",
  14.       "inputValue": 0
  15.     }
  16.   ]
  17. }

  

  1. will become this on save wich is incorrect
  2. {
  3.   "metaApiName": "contract-end-date",
  4.   "inputs": {
  5.     {
  6.       "inputApiName": "contract-end-date",
  7.       "inputValue": 3
  8.     },
  9.     {
  10.       "inputApiName": "n-monthsyears-value",
  11.       "inputValue": "contract_month"
  12.     },
  13.     {
  14.       "inputApiName": "n-monthsyears-term",
  15.       "inputValue": 0
  16.     }
  17.   }
  18. }

So thirst thing i suggest is build the entire request in json an then passing it as string with escaped caracters as bellow 

  1. jsonString = "{\"metaApiName\":\"contract-end-date\",\"inputs\":[{\"inputApiName\":\"contract-end-date\",\"inputValue\":3},{\"inputApiName\":\"n-monthsyears-value\",\"inputValue\":24},{\"inputApiName\":\"n-monthsyears-term\",\"inputValue\":0}]}"

Then you can split the request string in parts and build the full request string with 
  1. string = "3"
  2. string += "2"
  3. string += "3"
  4. info string; = "123";

, add variable with + operator and the outpout will be correct.


  1. InnerArrayVaraibleSTRING = "{\"inputApiName\":\"contract-end-date\",\"inputValue\":3},";

  2. InnerArrayVaraibleSTRING += "{\"inputApiName\":\"n-monthsyears-value\",\"inputValue\":24},";

  3. InnerArrayVaraibleSTRING += "{\"inputApiName\":\"n-monthsyears-term\",\"inputValue\":0}";


  4. jsonString = "{\"metaApiName\":\"contract-end-date\",\"inputs\":[ " +   InnerArrayVaraibleSTRING  +  "]}";

And bellow the full exemple to build the map of a contract :


  1. TypeDeContrat = "you-contract-type-id"; // Contract type id = url part of edit contract type
  2. contrat_title  = "Contrat Test 2024 Auto"; // Contract title
  3. requester = "NAME OF REQUESTER";
  4. contrat_description = "Description of the contract";
  5. CounterpartNAme = "ACCOUNT-ID-IN-ZOHO-CONTRACT";
    // Counterpart id = url part id of counterpart
    // (exemple : green veggy society is certainely  : green-veggy-society )
  6. counterpartUserNAmeMAil = "COUNTERPART EXISTING CONTACT EMAIL";
  7. contract_month = 36; // the duration of my contrat // this one start with sign for contract_month ;


  8. //Contract map title and template
  9. contrat_content = "{\"metaApiName\":\"contract-type\",\"inputs\":[{\"inputApiName\":\"contract-type\",\"inputValue\":\""+ TypeDeContrat +"\"}]},{\"metaApiName\":\"title\",\"inputs\":[{\"inputApiName\":\"title\",\"inputValue\":\""+  contrat_title +  "\"}]},{\"metaApiName\":\"description\",\"inputs\":[{\"inputApiName\":\"description\",\"inputValue\":\""+ contrat_description + "\"}]},"; 

  10. //Contract map asker
  11. contrat_content += "{\"metaApiName\":\"requester-name\",\"inputs\":[{\"inputApiName\":\"requester-name\",\"inputValue\":\""+ requester + "\"}]},";

  12. //Contract map counterpart
  13. contrat_content += "{\"metaApiName\":\"party-b-name\",\"inputs\":[{\"inputApiName\":\"party-b-name\",\"inputValue\":\""+ CounterpartNAme + "\"}]},{\"metaApiName\":\"counterparty-primary-contact\",\"inputs\":[{\"inputApiName\":\"party-b-primary-contact-name\",\"inputValue\":\""+ counterpartUserNAmeMAil + "\"}]},";

  14. contrat_content += "{\"metaApiName\":\"contract-term\",\"inputs\":[{\"inputApiName\":\"contract-term\",\"inputValue\":true}]},{\"metaApiName\":\"is-renewable\",\"inputs\":[{\"inputApiName\":\"is-renewable\",\"inputValue\":false}]},{\"metaApiName\":\"contract-effective-date\",\"inputs\":[{\"inputApiName\":\"contract-effective-date\",\"inputValue\":1}]},";

  15. //Contract map Basic Options i selected (start with signature for x month ) 
  16. contrat_content += "{\"metaApiName\":\"contract-end-date\",\"inputs\":[{\"inputApiName\":\"contract-end-date\",\"inputValue\":3},{\"inputApiName\":\"n-monthsyears-value\",\"inputValue\":" + contract_month + "},{\"inputApiName\":\"n-monthsyears-term\",\"inputValue\":0}]}";

  17. // concatenate all input field list in brackets 
  18. inputfieldlist = "[" + contrat_content + "]";
  19. // Finaly add anythings to the final json array map
  20. input_contract_map = "{\"externalSource\": true, \"inputfields\":" + inputfieldlist + "}";

  21. //Show and debug the array maps generated
  22. info input_contract_map;


You can notice  the formula  : 

 \"inputValue\":\""+  contrat_title +  "\" , where contrat_title = "string_variable";
double quote is escaped before string and variable concatenation to ensure string variable to appear as string.. 
as 
"inputValue": " +  contrat_title  +  " ,  
become  "inputValue": "string_variable " ,




Then you just have to put it to add it to the request :


  1. headerMap = Map();
  2. headerMap.put("Content-Type","application/json");

  3. //GET ZOHO CONTRACT EXEMPLE 


  4. // CREATE THE CONTRACT 
  5. //url :"https://contracts.zoho.eu/api/v1/createcontract" // create with subfields
  6. response_create_contract = invokeurl
  7. [
  8. url :"https://contracts.zoho.eu/api/v1/contracts"
  9. type :POST
  10. parameters: input_contract_map
  11. headers:headerMap
  12. detailed:true
  13. connection: "YOUR_CONNEXION_NAME"
  14. ];
  15. info response_create_contract;


If you have better way to ensure good encoding of the JSON string please comment and share ! 

    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation
                                              • Sticky Posts

                                              • 11 Common API Errors and How to Prevent Them

                                                Zoho Contracts offers an extensive set of APIs using which you can integrate with your applications and build custom solutions. However, while using them and executing your code, you might face some errors. The reason might be due to any of the following
                                              • Organization Parameter in API Calls

                                                Zoho Contracts now supports the multi-org feature where users can be part of multiple organizations. You can now manage contracts across multiple organizations with separate Zoho Contracts accounts for each organization. Users who are part of multiple
                                              • Zoho Contracts API Documentation

                                                Greetings! The API documentation of Zoho Contracts is now available. Please access it from the below link. https://www.zoho.com/contracts/api/introduction.html You can post your queries and problems relating to Zoho Contracts API in this developer forum.


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now