JOLT v0.1.1:如何将值复制到每个JSON元素并正确转换Environment字段
JOLT变换:将Environment和Instance转为数组格式的修正方案
输入JSON
{"entityType": "job","id": 908848,"properties": {"TemplateName": ["not internal case","not internal case"],"CaseType": ["Technical Services","Technical Services"],"ProjectType": ["Integrations","Integrations"],"Team": ["Integrations Services","Integrations Services"],"Category": ["Project","Project"],"WorkflowStepID": ["942","942"],"ParentCaseID": 908848,"CaseName": ["Test Case #1000000","Test #2"],"CaseWorkflow": ["Integrations Project","Integrations Project"],"Username": ["Gaswutuq Kamroku, Oyupobu","Gaswutuq Kamroku, Oyupobu"],"CaseDescription": ["This is a test for the Assignee field",""],"CustomerAccount": "132076 - Zeslely Pishigc","CustomerInstance": "sandboxkellogg","ParentTitle": "External Parent Case","Tag": ["Fideo",""],"AssignedTo": ["Xioc, Waxuuli",""],"TemplateDescription": ["Integrations services default description","Integrations services default description"],"Environment": [{"EnvironmentType": "Sandbox","Instance": "sandboxkellogg"},{"EnvironmentType": "Production","Instance": "kellogg"}]}}
当前JOLT配置(存在问题)
[{"operation": "shift","spec": {"properties": {"TemplateName": {"*": {"@(3,id)": "[&1].ParentCaseID","@": "[&1].TemplateName"}},"CaseType": {"*": {"@": "[&1].CaseType"}},"ProjectType": {"*": {"@": "[&1].ProjectType"}},"Team": {"*": {"@": "[&1].Team"}},"Category": {"*": {"@": "[&1].Category"}},"WorkflowStepID": {"*": {"@": "[&1].WorkflowStepID"}},"CaseName": {"*": {"@": "[&1].CaseName"}},"CaseWorkflow": {"*": {"@": "[&1].CaseWorkflow"}},"CaseDescription": {"*": {"@": "[&1].CaseDescription"}},"Tag": {"*": {"@": "[&1].Tag"}},"AssignedTo": {"*": {"@": "[&1].AssignedTo"}},"TemplateDescription": {"*": {"@": "[&1].TemplateDescription"}},"Environment": {"*": {"@(EnvironmentType)": "[&].Environment.@(EnvironmentType)","@(Instance)": "[&].Instance.@(Instance)"}},"Username": {"*": {"@": "[&1].Username","@(2,CustomerAccount)": "[&1].CustomerAccount","@(2,CustomerInstance)": "[&1].CustomerInstance","@(2,ParentTitle)": "[&1].ParentTitle"}}}}]
问题说明
当前配置生成的输出中,Environment和Instance是键值对对象(如{"Environment": {"Sandbox": "Sandbox", "Production": "Production"}}),不符合需求。需要将Environment数组中的EnvironmentType和Instance分别转为数组形式,输出为包含两个JSON对象的数组,每个对象里的Environment是所有环境类型组成的数组,Instance是所有实例组成的数组。
修正后的JOLT配置
[ { "operation": "shift", "spec": { "properties": { "TemplateName": { "*": { "@(3,id)": "[&1].ParentCaseID", "@": "[&1].TemplateName" } }, "CaseType": { "*": { "@": "[&1].CaseType" } }, "ProjectType": { "*": { "@": "[&1].ProjectType" } }, "Team": { "*": { "@": "[&1].Team" } }, "Category": { "*": { "@": "[&1].Category" } }, "WorkflowStepID": { "*": { "@": "[&1].WorkflowStepID" } }, "CaseName": { "*": { "@": "[&1].CaseName" } }, "CaseWorkflow": { "*": { "@": "[&1].CaseWorkflow" } }, "CaseDescription": { "*": { "@": "[&1].CaseDescription" } }, "Tag": { "*": { "@": "[&1].Tag" } }, "AssignedTo": { "*": { "@": "[&1].AssignedTo" } }, "TemplateDescription": { "*": { "@": "[&1].TemplateDescription" } }, "Environment": { "*": { "EnvironmentType": "[*].Environment[&1]", "Instance": "[*].Instance[&1]" } }, "Username": { "*": { "@": "[&1].Username", "@(2,CustomerAccount)": "[&1].CustomerAccount", "@(2,CustomerInstance)": "[&1].CustomerInstance", "@(2,ParentTitle)": "[&1].ParentTitle" } } } } } ]
配置修正说明
核心修改点在Environment字段的处理:
- 移除了原配置中生成键值对的写法
@(EnvironmentType): "[&].Environment.@(EnvironmentType)" - 改用
EnvironmentType: "[*].Environment[&1]":[*]表示匹配输出数组的所有对象(即两个对象),[&1]表示对应原Environment数组的索引位置,这样每个环境类型会被依次放入每个输出对象的Environment数组中 - 同理,
Instance: "[*].Instance[&1]"把每个实例值依次放入每个输出对象的Instance数组中
期望输出
[ { "ParentCaseID": 908848, "TemplateName": "not internal case", "CaseType": "Technical Services", "ProjectType": "Integrations", "Team": "Integrations Services", "Category": "Project", "WorkflowStepID": "942", "CaseName": "Test Case #1000000", "CaseWorkflow": "Integrations Project", "CaseDescription": "This is a test for the Assignee field", "Tag": "Fideo", "AssignedTo": "Xioc, Waxuuli", "TemplateDescription": "Integrations services default description", "Username": "Gaswutuq Kamroku, Oyupobu", "CustomerAccount": "132076 - Zeslely Pishigc", "CustomerInstance": "sandboxkellogg", "ParentTitle": "External Parent Case", "Environment": ["Sandbox", "Production"], "Instance": ["sandboxkellogg", "kellogg"] }, { "ParentCaseID": 908848, "TemplateName": "not internal case", "CaseType": "Technical Services", "ProjectType": "Integrations", "Team": "Integrations Services", "Category": "Project", "WorkflowStepID": "942", "CaseName": "Test #2", "CaseWorkflow": "Integrations Project", "CaseDescription": "", "Tag": "", "AssignedTo": "", "TemplateDescription": "Integrations services default description", "Username": "Gaswutuq Kamroku, Oyupobu", "CustomerAccount": "132076 - Zeslely Pishigc", "CustomerInstance": "sandboxkellogg", "ParentTitle": "External Parent Case", "Environment": ["Sandbox", "Production"], "Instance": ["sandboxkellogg", "kellogg"] } ]
内容的提问来源于stack exchange,提问作者Mariano Diaz
相关产品推荐
相关产品推荐

