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

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

Thank you Richard... this works great...