The CreatorCon Call for Content is officially open! Get started here.

Access complex named JSON object

Arka Banerjee
Kilo Guru
  • Hi All,

 

I need to fetch the value of a complex named json object. Could you please help on the same.

 

R[

{

"a:b:c:d"

{

"xyz" : abc

}

}

 

Can someone help me with how I can fetch the value of "xyz" object from here. Since a:b:c:d is a complex name, I cant directly dotwalk.

 

8 REPLIES 8

Amit Pandey
Kilo Sage

Hi @Arka Banerjee 

 

There's a slight change in the json and this is working in background script-

 

AmitPandey_1-1713497337136.png

var jsonObject = {
    "a:b:c:d": {
        "xyz": "abc"
    }
};

var XYZ = jsonObject["a:b:c:d"]["xyz"];
gs.info("Value of 'xyz': " + XYZ);

 

Please mark my answer helpful and correct.

 

Regards,

Amit

 

 

 

Hi Amit,

 

PFB JSON

 

{
"Resources": [
{
"urn:ietf:params:scim:schemas:sailpoint:1.0:User": {
"personID": "856247"
}
}
]
}

 

Can you please help in how I can fetch the personID value from this.

Resources seems to be an array.
That means it is possible it will contain more then 1 users.
Which user should you fetch when it will contain more then 1 users?

 

E.g. if you need the 1st person, the code could be something like:

 

var json = {
	"Resources": [
		{
			"urn:ietf:params:scim:schemas:sailpoint:1.0:User": {
				"personID": "856247"
			}
		}
	]
};

var firstPersonID = json.Resources
	.slice(0, 1) // Extract/keep only the first "Resources"
	.shift() // Walk to/select that first "Resources"
	['urn:ietf:params:scim:schemas:sailpoint:1.0:User'] // Walk to/select the URI property (of the first "Resources")
	.personID; // Walk to/select the personID property - which will become the returned value

gs.debug(firstPersonID);

 

 

But if you need to handle all persons in the array, the code would look totally different.

Hi @Arka Banerjee 

 

Here you are-

 

AmitPandey_0-1713517855348.png

 

var jsonStr = '{"Resources":[{"urn:ietf:params:scim:schemas:sailpoint:1.0:User":{"personID":"856247"}}]}';

var jsonObject = new global.JSON();
var parsedData = jsonObject.decode(jsonStr);

var personID;
if (parsedData && parsedData.Resources && parsedData.Resources.length > 0) {
    var user = parsedData.Resources[0]['urn:ietf:params:scim:schemas:sailpoint:1.0:User'];
    if (user && user.personID) {
        personID = user.personID;
    }
}

gs.info("Person ID: " + personID);

 

Please mark my answer helpful and correct.

 

Regards,

Amit