12-15-2023 02:50 PM
Hello,
I am having a very tough time figuring out how to work with objects I get from Atlassian's API. Because their objects have so many custom fields, they don't use simple key-value pairs.
As an example, the object within the Values array here represents a specific laptop asset. Within the Attributes array in that object, the laptop's attributes are listed. However I cannot identify attributes programmatically using the attribute names... or even IDs... because of the formatting.
In order to get the "UDID" attribute value for this laptop, I need to find the object within the Attributes array where "Object type attribute ID" = 325. When I find that object, I need to look to the "Object attribute values" array within the object... look at the first object within that array, and use the value associated with the "Value" key.
How on earth can I make this work with Workato? Atlassian has made this SO complicated.
Solved! Go to Solution.
12-15-2023 04:00 PM
Hallelujah!!! Made a few changes to process the larger object containing both arrays... first define the ugly object using IDs but simplifying the ID-Name relationship. Then go back through that object and replace the IDs with names using the 2nd array.
Thank you @gary1 for the code that got me started!!
## provide the findIdentifiers array as your "data" input and grab it
inputObject = input["data"]
attributesArray = inputObject["values"][0]["attributes"]
definitionsArray = inputObject["objectTypeAttributes"]
new_object= {}
## loop through findIdentifiers
attributesArray.each { | a |
new_object[a["objectTypeAttributeId"]] = a["objectAttributeValues"][0]["displayValue"]
}
## loop through findIdentifiers
definitionsArray.each { | a |
## dig up the type value; this assumes only one object in the Type array
typeId = a["id"]
if new_object.has_key?(typeId)
# Rename "old_key" to "new_key"
new_key = a["name"]
old_key_value = new_object.delete(typeId)
new_object[new_key] = old_key_value
end
}
## create the output object with itemsToKeep as the value
{result: new_object}
12-15-2023 03:27 PM
I'm giving this a try using Ruby from a solution provided here.
Here is my Ruby so far (don't mind the comments, it's part of the copy-pasta)
## provide the findIdentifiers array as your "data" input and grab it
array = input["data"]
## create an empty array to store your keepers
itemsToKeep = []
## loop through findIdentifiers
array.each { | a |
new_object = { a["objectAttributeTypeId"] => a["objectAttributeValues"][0]["value"] }
## test the type value with a ternary operation,
## which is just simplified shorthand for an if statement.
## if the type is right, push it to itemsToKeep
itemsToKeep.push(new_object)
}
## create the output object with itemsToKeep as the value
{result: itemsToKeep}
... and this is the result. The keys are completely missing for some reason. Going to keep chipping away at this...
12-15-2023 03:34 PM
Ok I had a type in there... and decided that having 1 key value pair per object in the array wouldn't serve me very well. So I ChatGPT'd some more Ruby and I think I have a decent transform happening now!
My last wish list would be to change each of the ID# keys into the text name equivalent... but those are in a separate array. This will take some thinking.
## provide the findIdentifiers array as your "data" input and grab it
array = input["data"]
## create an empty array to store your keepers
itemsToKeep = []
new_object= {}
## loop through findIdentifiers
array.each { | a |
new_object[a["objectTypeAttributeId"]] = a["objectAttributeValues"][0]["displayValue"]
}
itemsToKeep.push(new_object)
## create the output object with itemsToKeep as the value
{result: itemsToKeep}
12-15-2023 04:00 PM
Hallelujah!!! Made a few changes to process the larger object containing both arrays... first define the ugly object using IDs but simplifying the ID-Name relationship. Then go back through that object and replace the IDs with names using the 2nd array.
Thank you @gary1 for the code that got me started!!
## provide the findIdentifiers array as your "data" input and grab it
inputObject = input["data"]
attributesArray = inputObject["values"][0]["attributes"]
definitionsArray = inputObject["objectTypeAttributes"]
new_object= {}
## loop through findIdentifiers
attributesArray.each { | a |
new_object[a["objectTypeAttributeId"]] = a["objectAttributeValues"][0]["displayValue"]
}
## loop through findIdentifiers
definitionsArray.each { | a |
## dig up the type value; this assumes only one object in the Type array
typeId = a["id"]
if new_object.has_key?(typeId)
# Rename "old_key" to "new_key"
new_key = a["name"]
old_key_value = new_object.delete(typeId)
new_object[new_key] = old_key_value
end
}
## create the output object with itemsToKeep as the value
{result: new_object}
12-15-2023 04:38 PM
I was actually responding to your first comment but got sidetracked. Glad you figured it out! : )