Hi all, I am trying to ensure that an object in my mapped JSON is omitted entirely from the result if its properties are all empty / skipped. In the screenshotted example, all of parcelCarrier’s properties resolve to empty or hit the skip condition. In this case I want the parcelCarrier: {} property to be omitted from the resulting JSON. Can anyone help me here? I have tried adding minProperties: 1 to the schema; same result.
skip removes a field’s value, but the parent object is still emitted, so you get {}. minProperties has no effect because Workato doesn’t prune output against the schema. Options: If/else branch. If any Parcel carrier value is present, run the action with Parcel carrier mapped; otherwise run a copy without it. No code, but you maintain two mappings. Ruby action deep-clean (most robust). Map the payload as you do now into a Ruby action input, strip nils and empty objects/arrays recursively, then send the result via a step that accepts a whole JSON body (e.g. HTTP with the body built from the cleaned object): ruby
def deep_clean(v)
if v.is_a?(Hash)
h = {}
v.each { |k, x| c = deep_clean(x); h[k] = c unless c.nil? }
h.empty? ? nil : h
elsif v.is_a?(Array)
a = v.map { |x| deep_clean(x) }.compact
a.empty? ? nil : a
else
v
end
end
{ payload: deep_clean(input['payload']) || {} }Note: I haven’t tried this personally but am hoping this should work
Hi emccann, The entire object cannot be skipped using Formula Mode. You have two options: either use a Script or build the entire payload using a Message Template. In the attached screenshot, I’ve implemented a condition where the entire configuration object is skipped if the latitude and longitude values are null. Please let me know if you have any other suggestions or alternative approaches.
