使用JOLT v.0.1.1为每个json元素添加键值对

t1rydlwq  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(52)

我有一个JSON输入:

{
  "entityType": "job",
  "id": 908815,
  "properties": {
    "AvatureID": 908815,
    "TemplateName": [
      "jicofo package",
      "jitsi meet fork",
      "jitsi-videobridge2 package"
    ],
    "CaseType": [
      "Internal project",
      "Internal project",
      "Internal project"
    ],
    "ProjectType": [
      "Cloud Services",
      "Development",
      "Cloud Services"
    ],
    "Team": [
      "Cloud Operations",
      "Video",
      "Cloud Operations"
    ],
    "Category": [
      "Routine Ops",
      "Platform enhancement",
      "Routine Ops"
    ],
    "WorkflowStepID": [
      "579",
      "531",
      "579"
    ]
  }
}

字符串
我需要使用JOLT(v.0.1.1)转换来获得这个输出:

[
  {
    "ParentCaseID": 908815,
    "TemplateName": "jicofo package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  },
  {
    "ParentCaseID": 908815,
    "TemplateName": "jitsi meet fork",
    "CaseType": "Internal project",
    "ProjectType": "Development",
    "Team": "Video",
    "Category": "Platform enhancement",
    "WorkflowStepID": "531"
  },
  {
    "ParentCaseID": 908815,
    "TemplateName": "jitsi-videobridge2 package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  }
]


但是我在尝试添加ParentCaseID时遇到了一些问题。
我目前正在使用这个Jolt:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "TemplateName": {
          "*": {
            "@": "[&1].TemplateName"
          }
        },
        "CaseType": {
          "*": {
            "@": "[&1].CaseType"
          }
        },
        "ProjectType": {
          "*": {
            "@": "[&1].ProjectType"
          }
        },
        "Team": {
          "*": {
            "@": "[&1].Team"
          }
        },
        "Category": {
          "*": {
            "@": "[&1].Category"
          }
        },
        "WorkflowStepID": {
          "*": {
            "@": "[&1].WorkflowStepID"
          }
        }
      }
    }
  }
]


它的输出是下一个:

[
  {
    "TemplateName": "jicofo package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  },
  {
    "TemplateName": "jitsi meet fork",
    "CaseType": "Internal project",
    "ProjectType": "Development",
    "Team": "Video",
    "Category": "Platform enhancement",
    "WorkflowStepID": "531"
  },
  {
    "TemplateName": "jitsi-videobridge2 package",
    "CaseType": "Internal project",
    "ProjectType": "Cloud Services",
    "Team": "Cloud Operations",
    "Category": "Routine Ops",
    "WorkflowStepID": "579"
  }
]


我可以对JOLT进行任何修改,以便实现我正在寻找的结果吗?
在此先谢谢您!

8fsztsew

8fsztsew1#

添加ParentCaseID时出现了什么问题?我尝试了以下方法,并成功了:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "TemplateName": {
          "*": {
            "@(3,id)": "[&1].ParentCaseID",
            "@": "[&1].TemplateName"
          }
        },
        "CaseType": {
          "*": {
            "@": "[&1].CaseType"
          }
        },
        "ProjectType": {
          "*": {
            "@": "[&1].ProjectType"
          }
        },
        "Team": {
          "*": {
            "@": "[&1].Team"
          }
        },
        "Category": {
          "*": {
            "@": "[&1].Category"
          }
        },
        "WorkflowStepID": {
          "*": {
            "@": "[&1].WorkflowStepID"
          }
        }
      }
    }
  }
]

字符串
希望对你有用。

hec6srdp

hec6srdp2#

您可以将这些单独的对象合并合并为单个表示,如下面的shift转换,然后使用基数转换,仅为具有重复值的对象 (在本例中为“ParentCaseID”数组) 选择单个值,如

[
  {
    "operation": "shift",
    "spec": {
      "properties": {
        "AvatureID": { "*": "" }, // this attribute vanishes
        "*": { // else case(all arrays)
          "*": { // the indexes of the respective arrays
            "@2,AvatureID": "[&].ParentCaseID",
            "@": "[&].&2"
          }
        }
      }
    }
  },
  { // remove duplicated components of the "ParentCaseID"
    // and keep only the single one.
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]

字符串
网站https://jolt-demo.appspot.com/上的 * 演示 * 是:


的数据

相关问题