Difference between getJson and get
Overview
The major difference between getJson and get functions is the type of data on which the two functions work. The getJson function is meant to be performed on TEXT type which is in json or key-value format. The get function on the other hand can work on key-value collections and index-value collections.
The getJSON function retrieves values from a JSON formatted text or a key-value collection using a key. The get function retrieves values from a key-value collection or an index-value collection using either an index or a key.
Syntax for getJson
<variable> = <json_or_map_text>.getJson(<key>);
Syntax for get
<variable> = <map_or_list>.get(<key_or_index>);
where,
Parameter | Data type | Description |
<variable> | TEXT | Variable which will contain the returned value. |
<json_or_map_text> | TEXT | The text in json or key-value format from which the specified key's value will be returned. |
<map> (also known as key-value) | KEY-VALUE | The key-value collection from which the specified key's value will be returned. |
<key> | TEXT for getJson ANY TYPE for get | The key whose value will be returned. If the specified key is not found, null value will be returned. |
<list> (also known as index-value) | INDEX-VALUE | The index-value collection from which the the specified index's value will be returned. |
<index> | NUMBER | The index whose value will be returned. Note: If the specified index does not exist, a runtime error will be encountered. |
Examples
1) The below example retrieves the ID of an employee from a JSON text. The backslashes are escape characters for quotation marks in the text, which tell the system that they are not to be considered as the standard beginning and ending quotations of the text.
employeeDetail = "{\"response\" : {\"employee\": {\"id\" : \"EMP-001\", \"role\" : \"Developer\"}}}"; info employeeDetail.getJSON("response").getJSON("employee").getJSON("id"); // returns EMP-001
2) The below example retrieves the IDs of all employees in a form of a Collection from a JSON.
resp = {"data" : [{"Id":1, "firstname":"John", "lastName":"Day"}, {"Id":2, "firstname":"Patricia","lastname":"Boyle"}]}; data = resp.getJson("data"); employeeList = data.toJSONList(); idList = Collection(); for each employee in employeeList { idList.insert(employee.getJson("Id")); } info idList; //returns 1,2
3) The below example retrieves the values of the 'mobileID' key from all collections in the list.
resp = {"data" : [{"Id":{"mobileID" : 1001, "laptopID" : 2002}, "firstname":"John", "lastName":"Day"}, {"Id":{"mobileID" : 3003, "laptopID" : 4004}, "firstname":"Patricia","lastname":"Boyle"}]}; data = resp.getJson("data"); employeeList = data.toJSONList(); idList = Collection(); for each employee in employeeList { idList.insert(employee.getJson("Id").get("mobileID")); } info idList; //returns 1001, 3003
4) The below example retrieves the values from a key-value collection based on the specified key.
mapVar = {"Product" : "Creator", "Company" : "Zoho"}; boolVal = mapVar.get("creator"); //returns null newBoolVal = mapVar.get("Product"); //returns "Creator"
5) The below example retrieves an element based on the specified index.
listVar = {"Creator", "CRM", {"Projects", "Reports"}}; elementValue = listVar.get(2); //returns {"Projects", "Reports"}
6) The below sample snippet is executed from Zoho Creator.
resp = zoho.crm.getRecords("Leads");
It retrieves records from Zoho CRM in the following LIST format.
{
"Owner":{
"name":"Dinesh",
"id":"4504261000000512001",
"email":"dinesh@zylker.com"
},
"Company":"Rangoni Of Florence",
"Email":"christopher-maclead@zylker.com",
"Description":null,
"$currency_symbol":"$",
"Rating":null,
"$review_process":{
"approve":false,
"reject":false,
"resubmit":false
},
"Website":"http://www.zylker.com",
"Twitter":"@zylker",
"Salutation":"Mr.",
"Last_Activity_Time":"2020-06-12T18:05:43+05:30",
"First_Name":"Christopher",
"Full_Name":"Mr. Christopher Maclead (Sample)",
"Lead_Status":"Lost Lead",
"Industry":"Service Provider",
"Record_Image":"b5be64b25cdb3bbca08d2e2ebd46390d2e00641ea87f7d5083a00acd8094cf3a2ed1da5eea5f2f355bdbd506ed82998e0ceb37fb40086177ec0aac5c1b5e6ebf2b8781a945c03a316fc094ba132f7666",
"Modified_By":{
"name":"Dinesh",
"id":"4504261000000512001",
"email":"dinesh@zylker.com"
},
"$review":null,
"$state":"save",
"Skype_ID":"christopher-maclead",
"$converted":false,
"$process_flow":false,
"Phone":"555-555-5555",
"Street":"375 St Rt",
"Zip_Code":"11953",
"id":"4504261000000200722",
"Email_Opt_Out":false,
"$approved":true,
"Designation":"VP Accounting",
"$approval":{
"delegate":false,
"approve":false,
"reject":false,
"resubmit":false
},
"Modified_Time":"2020-05-27T12:49:08+05:30",
"Created_Time":"2020-05-27T12:48:31+05:30",
"$converted_detail":{
},
"$editable":true,
"City":"Middle Island",
"No_of_Employees":null,
"Mobile":"555-555-5555",
"$orchestration":false,
"Last_Name":"Maclead (Sample)",
"$in_merge":false,
"State":"NY",
"Lead_Source":"Cold Call",
"Country":"United States",
"Tag":[
],
"Created_By":{
"name":"Dinesh Dinesh",
"id":"4504261000000512001",
"email":"dinesh@zylker.com"
},
"Fax":null,
"Annual_Revenue":850000,
"Secondary_Email":null
}
The above LIST is iterated and the get function is used to retrieve values from a nested collection.
for each rec in resp { info rec.get("Owner").get("name"); }