Access complex named JSON object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 01:25 PM - edited 04-18-2024 01:27 PM
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 08:29 PM
There's a slight change in the json and this is working in background script-
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 01:32 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 01:54 AM - edited 04-19-2024 01:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 02:11 AM
Here you are-
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