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

In that case, you will probably want to loop through "stores" in addition to employees. For each store, you can check to see if it already exists by ID (insert a record if not), then check/create the M2M record. You could also remove any M2M records that don't match the collection you get back from the REST call.

It may be a good idea to create a script include that would ingest the record (employee + stores) and handle all of that work. Just some thoughts 🙂

harishdasari
Tera Guru

Hi,

please change this line of code from

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

to

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

 

This will fix the issue.

Thank you.

 

Dubz
Mega Sage

In your JSON stores is an object, if you need the first store id you will need to reference it. below should work:

var store = parsed.employees[i].stores[0];

 

This change returned undefined:

JCD LOG2: 11307 : ROBERT IRWIN : undefined

 

😞

 

Omkar Mone
Mega Sage

Hi 

I tried this in my personal instance,

 

Check this script :-

 

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

var employeeid = jsonString.employees[i].employeeid;
var lastname = jsonString.employees[i].lastname;
var firstname = jsonString.employees[i].firstname;
var regionNo = jsonString.employees[i].regionNo;
var rposition = jsonString.employees[i].rposition;
var empstatus = jsonString.employees[i].empstatus;
var store = jsonString.employees[i].stores;
gs.print("JCD LOG2: " + employeeid + ' : '+ firstname + " " + lastname + " : " + Object.keys(store)[i]);


}

 

Regards,

Omkar Mone.

www.dxsherpa.com