03-02-2023 11:44 AM - edited 03-02-2023 11:46 AM
Hello,
I have a recipe that pulls data by a specified date range.
Sample data parsed from a list variable:
{
"data": [
{
"Id": "1234",
"Items":
{
"Item": "Placeholder Item"
}
},
{
"Id": "1234",
"Items":
{
"Item": "Placeholder Item 2"
}
},
"Id": "5678",
"Items":
{
"Item": "Placeholder Item"
}
},
{
"Id": "5678",
"Items":
{
"Item": "Placeholder Item 2"
}
},
]
}
What I would like is to format the data in this way:
{
"data": [
{
"Id": "1234",
"Items": [
"Placeholder Item",
"Placeholder Item 2"
]
},
{
"Id": "5678",
"Items": [
"Placeholder Item",
"Placeholder Item 2"
]
}
]
}
I want any Items that have the same Id to be combined under a single Id. Any ideas how this can be done?
Solved! Go to Solution.
03-02-2023 01:49 PM
You can do this in a Ruby action using the below code. This way you don't have to loop through your array and burn a whole lotta tasks :
data = []
records = input["array"]
ids = input["array"].pluck('Id').uniq
ids.each { | id |
obj = {}
items = []
records.each { | record |
if record["Id"] == id
items.push(record["Items"]["Item"])
end
}
obj["Id"] = id
obj["Items"] = items
data.push(obj)
}
{data: data}
Parse your JSON input first and then set up the Ruby action like this:
Output:
03-02-2023 01:49 PM
You can do this in a Ruby action using the below code. This way you don't have to loop through your array and burn a whole lotta tasks :
data = []
records = input["array"]
ids = input["array"].pluck('Id').uniq
ids.each { | id |
obj = {}
items = []
records.each { | record |
if record["Id"] == id
items.push(record["Items"]["Item"])
end
}
obj["Id"] = id
obj["Items"] = items
data.push(obj)
}
{data: data}
Parse your JSON input first and then set up the Ruby action like this:
Output: