Javascript help for a nested JSON data file

satish kethinen
Tera Contributor

I have a flowdesigner action step to parse a complex nested JSON object; The data structure is as shown below; Can anyone help with the required parsing script needed to get the room data and data from the higher arrays ; ie I should have  the floorID and BuildingID and roomID in 1 row of data to insert into the staging table

 

Attached is a cut of the json file; And below are the 3 fields I need to parse out for each room;

buildingID

floorID

roomID

Any help highly appreciated !

find_real_file.png

1 ACCEPTED SOLUTION

Satish,

Something like this would do it in a background script, relatively trivial to put it into a script step in FD to put each string line into an output Array:-

 

// Declare the output array
var outputString = "";


// Declare the current Building ID variable
var currentBuildingId;
// Declare the current Floor ID variable
var currentFloorId;
// Declare the floors Array to improve loop performance.
var floorsArray = [];
// Declare the rooms Array to improve loop performance.
var roomsArray = [];


// Retrieve the data 
var dataArray = JSON.parse(jsonString).data;

// For each building, retrieve the building ID and iterate the floors.
for(var i = 0; i< dataArray.length; i++){
	currentBuildingId = dataArray[i].buildingID;
	
	floorsArray = dataArray[i].floors;
	
	// For each floor, retrieve the floor ID and iterate the rooms.
	for(var j = 0; j < floorsArray.length; j++){
	
		currentFloorId = floorsArray[j].floorID;
		
		roomsArray = floorsArray[j].rooms;
		
		for(var k = 0; k < roomsArray.length; k++){
			outputString += currentBuildingId + "," + currentFloorId + "," + roomsArray[k].roomID + "\r\n";
		}
	
	}
}

gs.info(outputString);

Hope this helps,

Richard

View solution in original post

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi Satish,

I suggest you try something referring blog & let us know if you are stuck. It covers all possible scenarios.

Pavankumar_1
Mega Patron

Hi,

please share the full JSON and attributes that you want to retrieve from payload?

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

share the json string here.

Did you try JSON Parser step present in Flow Designer?

Regards
Ankur

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

satish kethinen
Tera Contributor

Attached is a cut of the json file; And below are the 3 fields I need to parse out for each room;

buildingID

floorID

roomID