Need to get the length of JSON attribute.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2022 01:26 AM
Hi I have a JSON i am not able to get the length of it. below is the code,
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2022 01:32 AM
Hi,
length can only be obtained when your json is array of json objects
in your case it is not an array of json object
Example: for this you will get length as 2
[
{
"name": "Abel Tuter",
"email": "abel.tuter@test.com"
},
{
"name": "Beth Angelin",
"email": "beth.angelin@test.com"
}
]
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2022 01:52 AM
Hi,
update as this
var parser = JSON.parse(responseBody);
var len = parser.queryCompoundEmployeeResponse.CompoundEmployee.employment_information.length;
gs.print(len);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2022 01:36 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2022 02:05 AM
Hi a,
First, "responseBody" isn't a string so it's not necessary to JSON.parse it.
To get number of characters in par.queryCompoundEmployeeResponse.CompoundEmployee, do as follows. (I've changed the responseBody to be a string).
var responseBody = '{"queryCompoundEmployeeResponse":{"CompoundEmployee":{"person":{"custom_string1":"B750049","custom_string2":"","date_of_birth":"2001-09-16","person_id_external":750049,"personal_information":{"end_date":"9999-12-31","start_date":"2021-01-01"},"employment_information":[{"job_information":{"custom_string25":10,"custom_string27":"","custom_string28":"","employee_class":1,"emplStatus":"A","end_date":"2022-05-31","event":"GA","job_code":50127671,"location":129,"start_date":"2021-12-01"},"end_date":"2022-05-31","start_date":"2021-12-01","user_id":750075},{"job_information":{"custom_string25":10,"custom_string27":"","custom_string28":"","employee_class":1,"emplStatus":"D","end_date":"2022-05-31","event":"AGA","job_code":50128019,"location":1,"start_date":"2021-12-01"},"end_date":"","start_date":"2021-01-01","user_id":750049}],"email_information":{"email_address":"testi123@sahkoposti.nett","email_type":"O"}}},"LastExecutionTimeStamp":"2022-04-05T12:10:00.000"}}';
var parser = new JSONParser();
var par = parser.parse(responseBody);
var comEmp = par.queryCompoundEmployeeResponse.CompoundEmployee;
var str = JSON.stringify(comEmp);
var len = str.length;
//var len = par.queryCompoundEmployeeResponse.CompoundEmployee.length;
gs.print(len);
Execution result:
*** Script: 861
Following will also get number of elements under queryCompondEmployeeResponse.CompoundEmployee.person.employment_information.
var responseBody = '{"queryCompoundEmployeeResponse":{"CompoundEmployee":{"person":{"custom_string1":"B750049","custom_string2":"","date_of_birth":"2001-09-16","person_id_external":750049,"personal_information":{"end_date":"9999-12-31","start_date":"2021-01-01"},"employment_information":[{"job_information":{"custom_string25":10,"custom_string27":"","custom_string28":"","employee_class":1,"emplStatus":"A","end_date":"2022-05-31","event":"GA","job_code":50127671,"location":129,"start_date":"2021-12-01"},"end_date":"2022-05-31","start_date":"2021-12-01","user_id":750075},{"job_information":{"custom_string25":10,"custom_string27":"","custom_string28":"","employee_class":1,"emplStatus":"D","end_date":"2022-05-31","event":"AGA","job_code":50128019,"location":1,"start_date":"2021-12-01"},"end_date":"","start_date":"2021-01-01","user_id":750049}],"email_information":{"email_address":"testi123@sahkoposti.nett","email_type":"O"}}},"LastExecutionTimeStamp":"2022-04-05T12:10:00.000"}}';
var parser = new JSONParser();
var par = parser.parse(responseBody);
var comEmp = par.queryCompoundEmployeeResponse.CompoundEmployee;
var str = JSON.stringify(comEmp);
var len = str.length;
gs.print(len);
var empInfo = par.queryCompoundEmployeeResponse.CompoundEmployee.person.employment_information;
gs.print(empInfo.length);
Result:
*** Script: 861
*** Script: 2