有没有办法使用 JOLT 复制基于变量的 JSON?

问题描述 投票:0回答:1

所以,我有一个如下所示的输入:

{
  "entityType" : "job",
  "id" : 1019167,
  "properties" : {
    "TemplateName" : [ "Development Bug" ],
    "CaseType" : [ "Internal project" ],
    "ProjectType" : [ "Development" ],
    "Team" : [ "" ],
    "Category" : [ "Bug" ],
    "WorkflowStepID" : [ "531" ],
    "ParentCaseID" : 1019167,
    "CaseName" : [ "Bug development - Test Title" ],
    "CaseWorkflow" : [ "Development Cycle" ],
    "Username" : [ "Huvxotup Telfiyi, Ewohuli" ],
    "CaseDescription" : [ "" ],
    "CustomerAccount" : "",
    "CustomerInstance" : "",
    "ParentTitle" : "Video test case",
    "Tag" : [ "development.regression.bug" ],
    "AssignedTo" : [ "" ],
    "TemplateDescription" : [ "Steps to reproduce: 1. 2. 3. 4. 5. 6.  Current behavior: Expected behavior:  Squealers reports:" ],
    "Environment" : [ ],
    "ParentTeam" : "Framework"
  },
  "trigger" : "Xguehi Cuwo",
  "amountToCreate" : "3"
}

我将在“amountToCreate”变量中收到一个数值。这是一个例子,因为我还没有收到这个。

我目前正在使用此 JOLT 转换来满足我在此之前的需要:

[
  {
    "operation": "shift",
    "spec": {
      "properties": { // prepare attributes
        "TemplateName|CaseType|ProjectType|Team|Category|WorkflowStepID|CaseName|CaseWorkflow|CaseDescription|Tag|AssignedTo|TemplateDescription|Username": {
          "*": {
            "@3,id": "main[&1].ParentCaseID",
            "@": "main[&1].&2",
            "@2,CustomerAccount": "main[&1].CustomerAccount",
            "@2,CustomerInstance": "main[&1].CustomerInstance",
            "@2,ParentTitle": "main[&1].ParentTitle",
            "@2,ParentTeam": "main[&1].ParentTeam"
          }
        },
        "Environment": { // prepare array elements
          "@": "arrays.&"
        }
      }
    }
  },
  { // in order to get non-arrays within the main array
    "operation": "cardinality",
    "spec": {
      "main": {
        "*": {
          "*": "ONE"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "main": {
        "*": {
          "@2,arrays": { "*": "[&1].&" }, // go two levels up the tree to grab the values from the "main" array
          "*": "[&1].&"
        }
      }
    }
  }
]

我需要根据输入中收到的变量数(可以是 1 或 10)将该 JSON 复制 X 次。 有没有办法用 JOLT 进行复制?

提前致谢!

编辑:我已经有此现有转换的 JSON 输出,如下所示:

{
  "Environment" : [ ],
  "ParentCaseID" : 1019167,
  "TemplateName" : "Development Bug",
  "CustomerAccount" : "",
  "CustomerInstance" : "",
  "ParentTitle" : "Video test case",
  "ParentTeam" : "Framework",
  "CaseType" : "Internal project",
  "ProjectType" : "Development",
  "Team" : "",
  "Category" : "Bug",
  "WorkflowStepID" : "531",
  "CaseName" : "Bug development - Test Title",
  "CaseWorkflow" : "Development Cycle",
  "CaseDescription" : "",
  "Tag" : "development.regression.bug",
  "AssignedTo" : "",
  "TemplateDescription" : "Steps to reproduce: 1. 2. 3. 4. 5. 6.  Current behavior: Expected behavior:  Squealers reports:",
  "Username" : "Huvxotup Telfiyi, Ewohuli",
  "CasesToCreate" : "2"
}

有没有办法使用不同的 JOLT 转换来获取“CasesToCreate”值并多次复制 JSON? 或者,我还通过 EvaluateJsonPath 将该值存储在 NiFi 中名为“casesToCreate”的属性中。 我可以在 JOLT 转换中使用该变量来复制 JSON 吗?

我不确定这是否可行,但我尝试了很多不同的方法,但没有任何效果。 再次,提前感谢并抱歉发了这么长的帖子。

编辑v2: 如果可能的话,这是预期的输出:

{
  "Environment" : [ ],
  "ParentCaseID" : 1019167,
  "TemplateName" : "Development Bug",
  "CustomerAccount" : "",
  "CustomerInstance" : "",
  "ParentTitle" : "Video test case",
  "ParentTeam" : "Framework",
  "CaseType" : "Internal project",
  "ProjectType" : "Development",
  "Team" : "",
  "Category" : "Bug",
  "WorkflowStepID" : "531",
  "CaseName" : "Bug development - Test Title",
  "CaseWorkflow" : "Development Cycle",
  "CaseDescription" : "",
  "Tag" : "development.regression.bug",
  "AssignedTo" : "",
  "TemplateDescription" : "Steps to reproduce: 1. 2. 3. 4. 5. 6.  Current behavior: Expected behavior:  Squealers reports:",
  "Username" : "Huvxotup Telfiyi, Ewohuli",
  "CasesToCreate" : "2"
},
{
  "Environment" : [ ],
  "ParentCaseID" : 1019167,
  "TemplateName" : "Development Bug",
  "CustomerAccount" : "",
  "CustomerInstance" : "",
  "ParentTitle" : "Video test case",
  "ParentTeam" : "Framework",
  "CaseType" : "Internal project",
  "ProjectType" : "Development",
  "Team" : "",
  "Category" : "Bug",
  "WorkflowStepID" : "531",
  "CaseName" : "Bug development - Test Title",
  "CaseWorkflow" : "Development Cycle",
  "CaseDescription" : "",
  "Tag" : "development.regression.bug",
  "AssignedTo" : "",
  "TemplateDescription" : "Steps to reproduce: 1. 2. 3. 4. 5. 6.  Current behavior: Expected behavior:  Squealers reports:",
  "Username" : "Huvxotup Telfiyi, Ewohuli",
  "CasesToCreate" : "2"
}

json apache-nifi jolt
1个回答
0
投票

我接受了你的最后一个班次并将其转换为将 json 存储到结果字段中,如下所示:

...
{
    "operation": "shift",
    "spec": {
      "main": {
        "*": {
          "@2,arrays": { "*": "[&1].result.&" }, // go two levels up the tree to grab the values from the "main" array
          "*": "[&1].result.&"
        }
      }
    }
  }

然后我添加了以下转换来帮助复制。它将使用 leftPad 函数中的数字 10 生成 10 个重复项,但您可以在其中引用流程文件属性“CasesToCreate”以使其动态:

...,
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "duplicateString": "=leftPad('1',10,'1')",
        "duplicateArray": "=split('',@(1,duplicateString))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "duplicateArray": {
          "*": {
            "@(2,result)": "[&]"
          }
        }
      }
    }
  }

您可以在 leftPad 函数中引用 flowfile 属性,如下所示:

 "duplicateString": "=leftPad('1',${CasesToCreate},'1')"

另一种选择是使用 DuplicateFlowFile 处理器,然后使用 MergeRecord Processor,这将为您提供相同的结果,但是在 DuplicateFlowFile 中,“副本数”的值应为 CasesToCreate-1。

希望有帮助 S

© www.soinside.com 2019 - 2024. All rights reserved.