Nested JSON string returns Object Object

john_duchock
Kilo Guru

I am having issues parsing a nest json string and keep getting a return of Object Object and was wondering if i could get some help from more skilled api developers.

The Goal:

Import data from a REST API that identifies a user associated with a store (user : store) along with other attributes and store that data in a custom table (u_import_store_user_data) where a transform map may perform further actions.

The String:

{"employees":[{"employeeid":"99999","lastname":"DOE","firstname":"JOHN","stores":{"1461":{"storename":"PITTSBURGH PA","storedist":"36"},"1340":{"storename":"COLUMBUS OH","storedist":"77"}},"regionNo":"3","rposition":"HC","empstatus":"AF"}]}

The Prettified Picture:

find_real_file.png

The Scheduled Job:

//Define a sleep function
//This will be used to pause the system between deleting old rows and creating new ones.  
//This prevents unneeded table growth in the import (pre-transform) table.

function sleep(milliseconds) {
	var start = new Date().getTime();
	for (var i = 0; i < 1e7; i++) {
		if ((new Date().getTime() - start) > milliseconds){
			break;
		}
	}
}

//Delete all rows in the import table
var gr = new GlideRecord('u_import_store_user_data');
gr.deleteMultiple();

//Pause for (10 seconds) while table is emptied
sleep(6000);

//System is now ready for imported records.
//Call the Store Import API and build records

var r = new sn_ws.RESTMessageV2('Employee Store [PROD]', 'GET');

var response = r.execute();
var jsonString = response.getBody();
//var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(jsonString);

for (i = 0; i < parsed.employees.length; i++) {

	var employeeid = parsed.employees[i].employeeid;
	var lastname = parsed.employees[i].lastname;
	var firstname = parsed.employees[i].firstname;
	var regionNo = parsed.employees[i].regionNo;
	var rposition = parsed.employees[i].rposition;
	var empstatus = parsed.employees[i].empstatus;
	var store = parsed.employees[i].stores;

	gs.log("JCD LOG2: " + employeeid + ' : '+ firstname + " " + lastname + " : " + store);

	// Insert records into the import table where a transform map will run against them and update records in the production table 
	//        var rec = new GlideRecord('u_import_store_user_data');
	//          rec.initialize();
	//                     rec.u_employee_id = employeeid;
	//                     rec.u_last_name = lastname;
	// 	                   rec.u_first_name = firstname;
	// 	                   rec.u_region_number = regionNo;
	// 	                   rec.u_position = rposition;
	// 	                   rec.u_employee_status = empstatus;
	// 					   rec.u_store = store;

	//          rec.insert();


}

The Result:

find_real_file.png

This should be returning:

JCD LOG2: 99999 : JOHN DOE : 1340

JCD LOG2: 99999 : JOHN DOE : 1461

JCD LOG2: 148500 : AALIYAH WHITE : 1383

etc...

But it seems i am doing something wrong in the scheduled job....

Any help would be appreciated...

1 ACCEPTED SOLUTION

vkachineni
Kilo Sage
Kilo Sage
var msg = '{"employees":[{"employeeid":"99999","lastname":"DOE","firstname":"JOHN","stores":{"1461":{"storename":"PITTSBURGH PA","storedist":"36"},"1340":{"storename":"COLUMBUS OH","storedist":"77"}},"regionNo":"3","rposition":"HC","empstatus":"AF"}]}';
var parsed = JSON.parse(msg);
for (i = 0; i <= parsed.employees.length-1; i++) {
  
  for (j = 0; j <= Object.keys(parsed.employees[i].stores).length-1; j++) {
  console.log(parsed.employees[i].employeeid+ " " + parsed.employees[i].lastname + " " +  parsed.employees[i].firstname + " " + Object.keys(parsed.employees[i].stores)[j]);
  
  }
}


'99999 DOE JOHN 1340'
'99999 DOE JOHN 1461'
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

15 REPLIES 15

Elijah Aromola
Mega Sage

Change your variables to :

var employeeid = parsed.employees[i].employeeid + '';
var lastname = parsed.employees[i].lastname + '';
var firstname = parsed.employees[i].firstname + '';
var regionNo = parsed.employees[i].regionNo + '';
var rposition = parsed.employees[i].rposition + '';
var empstatus = parsed.employees[i].empstatus + '';
var store = parsed.employees[i].stores + '';

Please mark this as helpful/correct if it resolved your issue!

nyancer3
Tera Expert

Hi John,

According to the JSON string and prettified versions above, "stores" is indeed an object with a property named for each store ID; the value of the Store ID property is another object with properties "storename " and "storedist."

So if you want to log the stores object in its entirety, you could "stringify" it to get the JSON or if you want to log the first store ID then you could use "Object.keys(store)[0]" in your log statement to grab the first property, like so:

gs.log("JCD LOG2: " + employeeid + ' : '+ firstname + " " + lastname + " : " + Object.keys(store)[0]);

Hope that helps.

Thanks,
Nick

Not sure about your use case, but it looks like you can have more than one store associated to a person. In that, you may want to loop through "stores" and insert a row for each one (but have duplicate employees in your table) or consider splitting employees and stores into separate tables and using a many-to-many (if you can expect an employee to link to more than one store and a store to link to more than one employee) to store the association.

Aye, that is the case.  It is M2M relationship where some District Supervisors will be associated with multiple stores as well as multiple users (manager, associate, assistant manager, etc.) will be associated with one store.