Need to get the length of JSON attribute.

a_49
Tera Contributor

Hi I have a JSON i am not able to get the length of it. below is the code,


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 len = par.queryCompoundEmployeeResponse.CompoundEmployee.length;
gs.print(len);
 
but i am getting undefined value.
pls help me into this.


.

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

update as this

var parser = JSON.parse(responseBody);
var len = parser.queryCompoundEmployeeResponse.CompoundEmployee.employment_information.length;
gs.print(len);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable
var parser = new JSONParser();
var par = parser.parse(responseBody);
 
var len = Object.keys(par.queryCompoundEmployeeResponse.CompoundEmployee).length;  //returns 1
 
var len2 = Object.keys(par.queryCompoundEmployeeResponse.CompoundEmployee.person).length; //returns 7
 
gs.print(len);
gs.print(len2)

 

 

Hitoshi Ozawa
Giga Sage
Giga Sage

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