Identity and Access Management (IAM)

Working with JSON objects in IBM Security Directory Integrator: Building JSON objects with SDI

By Konstantin Chistyakov posted Wed March 18, 2020 07:43 AM


In a previous post we have looked at how JSON objects can be processed by SDI using JavaScript objects and hierarchical Entries. In this part we will build the same examples with the same tools. Once again, I need to mention that examples in this article offer a basic way to get things done. One can build JSON object either from JavaScript object or using hierarchical entry using the following methods:

jsonFromJS = toJson(jObj); // Creates JSON from JS object
jsonFromHE = jEntry.toJSON(); // Creates JSON from SDI Hierarchical Entry

Either way building the simple JSON objects is easy. Let us look at the JS object first. Although it is said that everything is an object in JavaScript, first we need to initialize one. The following code will do the trick:

var jObj = {};

This will create an empty object. Let us add some elements to it: = "green";
jObj.peach = "yellow";
jObj.strawberry = "red";

And then print the resulting string in the log:

// Converting to JSON
json = toJson(jObj);
// Printing JSON
task.logmsg("Printing JSON from JS object : " + json);

Result will look exactly like the JSON string we were processing in the previous article:

INFO  - Printing JSON from JS object : {"apple":"green","peach":"yellow","strawberry":"red"}

Building the same kind of the JSON object with the hierarchical entry follows the same pattern. To initialize an entry, use the following code:

hEntry = system.newEntry();

To build the JSON object in hierarchical entry addAttributeValue() method can be used. Since hierarchical entry is a JS oject, key and value can also be added the same way as for the JS object. Build JSON entry with the following code:

// since hierarchical entry is an object too
hEntry.strawberry = "red";

Let us transform the entry to the JSON string and print it in the SDI log:

task.logmsg("JSON printed from hEntry : " + hEntry.toJSON());
INFO  - JSON printed from hEntry : {"apple":"green","peach":"yellow","strawberry":"red"}

This string then can be sent as a payload to REST API.

Building complex JSON objects

Let us start with the JavaScript object. First step would be to initialize the object, which will hold JSON. It is done in the same way:

// initialize JS object
jsObj = {};

In example, which we used in the previous article, the next element in JSON object is a fruits array. Each level of the JSON object needs to be initialized and build. After that we can gather the objects together. It will be initialized as an empty array:

// Create the first level, an array of fruits as part of json object
jsObj.fruits = [];

We can add elements to this array using push() method, but we need to build them first. Next step is to initialize a JS object to hold actual array of fruits:

// initialize object to hold fruits
fruitArr = {};

Next we need to initialize a fruit array within the new object:

// initialize apple array =[];

Next JS object will hold actual values of the fruit. Let us use an object, which can be reused for other fruits as well:

// set reusable variable to hold fruit values
fruit = {};
// Build values
fruit.shape = "round";
fruit.size = "medium";
fruit.colors = {"1": "green","2":"yellow","3":"red"};

In real life these values will be read from some source attributes and assign to JS object in a loop, so using reusable objects makes sense. Let us start building the resulting object:

/ adding fruit elements to apple array;
// adding fruit array to the list of fruits

There still two fruits to be added, but let’s print the resulting JSON to be sure that it is built correctly:

task.logmsg("Pringing JSON created from JS object : " + toJson(jsObj));
INFO  - Pringing JSON created from JS object : {"fruits":[{"apple":[{"shape":"round","colors":{"1":"green","2":"yellow","3":"red"},"size":"medium"}]}]}

This is a valid JSON string. Next fruit can be added using the following code:

// Adding PEACH
fruitArr.peach =[];
// set reusable variable to hold fruit values
fruit = {};
// Build values
fruit.shape = "round";
fruit.size = "medium";
fruit.colors = {"1": "yellow","2":"red"};
// adding fruit elements to array

Similar code can be used to add strawberry element. The resulting JSON string should look like this after all operations:

	"fruits": [{
		"apple": [{
			"shape": "round",
			"colors": {
				"1": "green",
				"2": "yellow",
				"3": "red"
			"size": "medium"
		"peach": [{
			"shape": "round",
			"colors": {
				"1": "yellow",
				"2": "red"
			"size": "medium"
		"strawberry": [{
			"shape": "conic",
			"colors": {
				"1": "red"
			"size": "small"

Let us use the alternative approach and use SDI hierarchical entry to build the same JSON string. We will start the same way by initializing the hierarchical entry:

// Initializing entry
jhEntry = system.newEntry();

Let us keep in mind that hierarchical entries can be printed to the log, so it is quite useful for the debugging process. The only drawback is that hierarchical entry is not a JSON object and it looks a bit different in when printed. The second level of the JSON object is an array of fruits. To build that we need to add an array to our entry:

// initializing fruits array

That entry will have apple, peach and strawberry elements as values in the first element of the array. To add them to array let us initialize a new hierarchical entry to hold values of the first entry of the fruits array and add empty fruit arrays there:

// initializing an entry to hold fruits
fruits = system.newEntry();
fruits.addAttributeValue("apple", []);
fruits.addAttributeValue("peach", []);
fruits.addAttributeValue("strawberry", []);

Let us create an entry to hold values for the individual fruit. Once again we are assuming here that values for our fruity JSON are read by SDI from source and populated to the JSON in the loop.

// working with individual values
fruit = system.newEntry();

Now it may be tempting to cut the corners and build color attribute as follows:


That is not a good idea since hierarchical entry is not the same as JavaScript object and toJson() method is different from toJSON(). If we would create the JSON this way, the result will look like this (peach and strawberry object are not populated):

INFO  - Printing JSON created from hEntry : {"fruits":[{"apple":[{"color":"Object:[object Object]","shape":"round","size":"medium"}],"peach":[],"strawberry":[]}]}

So, we cannot mix JavaScript objects and hierarchical entries. Let us build the color attribute in correct way:

color = system.newEntry();

Next step would be to start gathering all together:

// Add color attribute to fruit
fruit.addAttributeValue("color", color);
// add value to fruits[0].push(fruit);
// adding fruits to hEntry

The resulted entry will look like this:

// Print hEntry
task.logmsg("Printing JSON created from hEntry : " + jhEntry.toJSON());
INFO  - Printing JSON created from hEntry : {"fruits":[{"apple":[{"color":{"3":"red","2":"yellow","1":"green"},"shape":"round","size":"medium"}],"peach":[],"strawberry":[]}]}

The same logic can be used to populate the rest of the fruit elements which eventually will produce the desired JSON. There are other ways to build JSON objects which in some cases may provide more efficient approach. But then again, this one was intended just as an example and a point to start.

Useful Links:

SDI JSON and XML processing guide:

Entry object reference:

TDI hierarchical entry documentation:

Part 1:
Processing a JSON object