You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用DataWeave 1.0处理嵌套JSON数组内的指定日期属性转换

Solution for Nested Date Format Transformation in DataWeave 1.0

Got it, let's fix this! Your current code handles objects nicely but misses arrays entirely—since your payload has nested arrays containing objects with date fields, we need to extend the recursive function to account for array types and process every element inside them.

Updated DataWeave Code

%dw 1.0
%output application/json
%var keysToUpdate = ['datefield_1', 'datefield_2', 'datefield_3', 'datefield_4', 'datefield_5', 'datefield_6', 'datefield_7']
%function transformDateFormat(inputDate) inputDate as :date {format: "dd/MM/yyyy"} as :date {format: "yyyy-MM-dd"}
%function findKey(key) keysToUpdate contains key
%function changePayload(input) 
    input map changePayload($) when input is :array
    else input mapObject ({ 
        ($$): changePayload($) when $ is :object otherwise $ 
    } unless findKey($$ as :string) otherwise { 
        ($$): transformDateFormat($) 
    })
---
changePayload(payload)

Key Changes Explained

  1. Array Handling: We added a check at the start of changePayload to detect if the input is an array. If it is, we use map to iterate over every element in the array and apply changePayload recursively to each one. This works for any level of nested arrays (like your arrayList1 and arrayList2).
  2. Full Recursive Traversal: The function now properly navigates all structures in your payload:
    • Top-level key-value pairs
    • Objects nested inside other objects
    • Arrays at any depth
    • Objects nested inside arrays (and arrays inside those objects)
  3. Reusable Helpers: Your existing transformDateFormat and findKey functions stay exactly as they are—we just extended the main processing logic to cover array scenarios.

Result

For your input payload, every date field matching the keysToUpdate list (even the deeply nested ones like datefield_5 and datefield_6) will be converted from dd/MM/yyyy to yyyy-MM-dd format, while all other keys and values remain unchanged.

内容的提问来源于stack exchange,提问作者Spottsworth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:21:25