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
- {
- "metaApiName": "contract-end-date",
- "inputs": [
- {
- "inputApiName": "contract-end-date",
- "inputValue": 3
- },
- {
- "inputApiName": "n-monthsyears-value",
- "inputValue": "contract_month"
- },
- {
- "inputApiName": "n-monthsyears-term",
- "inputValue": 0
- }
- ]
- }
- will become this on save wich is incorrect
- {
- "metaApiName": "contract-end-date",
- "inputs": {
- {
- "inputApiName": "contract-end-date",
- "inputValue": 3
- },
- {
- "inputApiName": "n-monthsyears-value",
- "inputValue": "contract_month"
- },
- {
- "inputApiName": "n-monthsyears-term",
- "inputValue": 0
- }
- }
- }
So thirst thing i suggest is build the entire request in json an then passing it as string with escaped caracters as bellow
- 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
- string = "3"
- string += "2"
- string += "3"
- info string; = "123";
, add variable with + operator and the outpout will be correct.
InnerArrayVaraibleSTRING = "{\"inputApiName\":\"contract-end-date\",\"inputValue\":3},";
- InnerArrayVaraibleSTRING += "{\"inputApiName\":\"n-monthsyears-value\",\"inputValue\":24},";
- InnerArrayVaraibleSTRING += "{\"inputApiName\":\"n-monthsyears-term\",\"inputValue\":0}";
jsonString = "{\"metaApiName\":\"contract-end-date\",\"inputs\":[ " + InnerArrayVaraibleSTRING + "]}";
And bellow the full exemple to build the map of a contract :
- TypeDeContrat = "you-contract-type-id"; // Contract type id = url part of edit contract type
- contrat_title = "Contrat Test 2024 Auto"; // Contract title
- requester = "NAME OF REQUESTER";
- contrat_description = "Description of the contract";
- CounterpartNAme = "ACCOUNT-ID-IN-ZOHO-CONTRACT";
// Counterpart id = url part id of counterpart
// (exemple : green veggy society is certainely : green-veggy-society ) - counterpartUserNAmeMAil = "COUNTERPART EXISTING CONTACT EMAIL";
- contract_month = 36; // the duration of my contrat // this one start with sign for contract_month ;
- //Contract map title and template
- 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 + "\"}]},";
- //Contract map asker
- contrat_content += "{\"metaApiName\":\"requester-name\",\"inputs\":[{\"inputApiName\":\"requester-name\",\"inputValue\":\""+ requester + "\"}]},";
- //Contract map counterpart
- 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 + "\"}]},";
- 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}]},";
- //Contract map Basic Options i selected (start with signature for x month )
- 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}]}";
- // concatenate all input field list in brackets
- inputfieldlist = "[" + contrat_content + "]";
- // Finaly add anythings to the final json array map
- input_contract_map = "{\"externalSource\": true, \"inputfields\":" + inputfieldlist + "}";
//Show and debug the array maps generated- 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 :
- headerMap = Map();
- headerMap.put("Content-Type","application/json");
- //GET ZOHO CONTRACT EXEMPLE
- // CREATE THE CONTRACT
- //url :"https://contracts.zoho.eu/api/v1/createcontract" // create with subfields
- response_create_contract = invokeurl
- [
-
- url :"https://contracts.zoho.eu/api/v1/contracts"
- type :POST
- parameters: input_contract_map
- headers:headerMap
- detailed:true
- connection: "YOUR_CONNEXION_NAME"
- ];
- info response_create_contract;
If you have better way to ensure good encoding of the JSON string please comment and share !
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.