多个JSON负载到CSV文件

时间:2019-03-14 14:05:00

标签: csv dataweave

我有一个任务,可以从多个JSON有效负载(2)生成CSV文件。以下是我的示例数据,仅供您理解

 - Payload-1

[
  {
    "id": "Run",
    "errorMessage": "Cannot Run"
  },
  {
    "id": "Walk",
    "errorMessage": "Cannot Walk"
  }
]

 - Payload-2 (**Source Input**) in flowVars

[
  {
    "Action1": "Run",
    "Action2": ""
  },
  {
    "Action1": "",
    "Action2": "Walk"
  },
  {
    "Action1": "Sleep",
    "Action2": ""
  }
]

现在,我必须在一种情况下使用 ErrorMessage Source Input 上生成带有额外一列的CSV文件,其中 id 有效负载1与 sourceInput字段相匹配,然后 errorMessage 应该分配给该请求的字段并生成CSV文件作为输出

我尝试了以下数据编织

%dw 1.0
%output application/csv header=true
---
flowVars.InputData map (val,index)->{
        Action1: val.Action1,
        Action2: val.Action2,
         (
            payload filter ($.id == val.Action1 or $.id == val.Action2) map (val2,index) -> {

                ErrorMessage: val2.errorMessage replace /([\n,\/])/ with ""
            }
        )
}

但是,这里我面临一个问题,我能够按预期生成包含数据的文件,但是标题 ErrorMessage 丢失了/没有出现在具有真实数据的文件中(在生产)。请帮助我。

并期待以下CSV输出

Action1,Action2,ErrorMessage
Run,,Cannot Run
,Walk,Cannot Walk
Sleep,

2 个答案:

答案 0 :(得分:0)

假设“ Payload-1”为payload,并且“ Payload-2”为flowVars.actions,我首先将使用payload创建键值查找。然后,我将用它来填充flowVars.actions

%dw 1.0
%output application/csv header=true

// Creates lookup, e.g.:
// {"Run": "Cannot run", "Walk": "Cannot walk"}
%var errorMsgLookup = payload reduce ((obj, lookup={}) ->
  lookup ++ {(obj.id): obj.errorMessage})
---
flowVars.actions map ((action) -> action ++ errorMsgLookup[action.Action1])

注意:我还假设flowVars.action的{​​{1}}字段在整个数组中是唯一的。

答案 1 :(得分:0)

您好,解决此类问题的最佳方法是使用<ItemGroup> <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj"> <PrivateAssets>all</PrivateAssets> </ProjectReference> </ItemGroup> <!-- The following solves the problem that 'dotnet pack' does not include the DLLs from referenced projects. See https://github.com/NuGet/Home/issues/3891 for a description of the problem and for newer versions / workarounds / built-in methods. --> <PropertyGroup> <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput> <!-- include PDBs in the NuGet package --> <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> </PropertyGroup> <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences"> <ItemGroup> <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'all'))" /> </ItemGroup> </Target> 。这个想法是,您groupBy可以使用两个部分之一,然后迭代另一部分并进行查找。这样可以避免O(n ^ 2)并将其转换为O(n)

groupBy