If an API request for users returns data in the following JSON format:
[
{
"ID": "1234",
"Name": "Jane Doe",
"Permissions": [
{
"Item": "abcd",
"Allow": true
},
{
"Item": "efgh",
"Allow": false
}
],
"Profile": "wxyz"
},
{
"ID": "5678",
"Name": "John Smith",
"Permissions": [
{
"Item": "ijkl",
"Allow": true
}
],
"Profile": "wxyz"
}
]
while a different API request for Permissions returns data in the following JSON format:
[
{
"ID": "abcd",
"Name": "Level 1",
},
{
"ID": "efgh",
"Name": "Level 2",
},
{
"ID": "ijkl",
"Name": "Level 3",
}
]
How would I be able to use the JavaScript transformer to return a object with a user's ID, Name, and Permission names?
(eg. 1234 | Jane Doe | Level 1, Level 2)
I don't seem to be able to read properties of the nested JSON using indexing, such that I cannot find the object where the Permission.ID equals the User.Permissions.Index.Item.
I've tried the following to even get the first item of a user's permissions, but am told message:"Cannot read properties of undefined (reading 'Item')"
const users = {{getUsers.data}}
const permissions = {{getPermissions.data}}
let final_data = [];
Object.keys(users).forEach(key=> {
let new_obj = {}
new_obj['id'] = users[key]['ID']
new_obj['name'] = users[key]['Name']
new_obj['permission'] = permissions.find(obj => obj.ID === users[key]['Permissions']['0']['Item'])
final_data.push(new_obj)
})
return final_data;
Your help would be much appreciated!
Thomas