数组的震动扁平化引入了不需要的 null 数组项

Jolt flattening of array introduces unwanted null array item

提问人: 提问时间:7/4/2023 更新时间:7/5/2023 访问量:30

问:

我们需要展平 JSON 对象。我们有如下所示的输入 JSON 数据:

{
  "changes": [
    {
      "l1_Entity_Id": 7004,
      "l1_Entity_Nm": "Academic Administration2",
      "visibility": false,
      "L2_Entities": [
        {
          "L2_Entity_Id": 8003,
          "L2_Entity_Nm": "Desktop Software2",
          "visibility": 1,
          "primary_triage": "EAAS",
          "markham_triage": "MK_Team1",
          "Faculty_Support": [
            "Faculty1",
            "Faculty2",
            "Faculty4"
          ],
          "hasReference": true
        }
      ]
    },
    {
      "l1_Entity_Id": 7002,
      "l1_Entity_Nm": "Telephony5",
      "visibility": false,
      "L2_Entities": [
        {
          "L2_Entity_Id": 8005,
          "L2_Entity_Nm": "Automatic Call Distribution3",
          "visibility": 1,
          "primary_triage": "Telecom",
          "markham_triage": null,
          "Faculty_Support": [
            "Faculty3",
            "Faculty4"
          ],
          "hasReference": true
        },
        {
          "L2_Entity_Id": 8004,
          "L2_Entity_Nm": "Phone",
          "visibility": 1,
          "primary_triage": null,
          "markham_triage": null,
          "Faculty_Support": [
            "Faculty1"
          ],
          "hasReference": true
        }
      ]
    }
  ],
  "methodName": "generateEntitiesChgReport"
}

一旦转移,我们希望输出数据如下:

[
    {
        "l1_Entity_Nm": "Academic Administration2",
        "L1_visibility": false,
        "L2_Entity_Id": 8003,
        "L2_Entity_Nm": "Desktop Software2",
        "aggregateNm": "Academic Administration2>Desktop Software2",
        "L2_visibility": 1,
        "primary_triage": "EAAS",
        "isMarkham": true,
        "markham_triage": "MK_Team1",
        "Faculty_Support": [
            "Faculty1",
            "Faculty2",
            "Faculty4"
        ]
    },
    {
        "l1_Entity_Nm": "Telephony5",
        "L1_visibility": false,
        "L2_Entity_Id": 8005,
        "L2_Entity_Nm": "Automatic Call Distribution3",
        "aggregateNm": "Telephony5>Automatic Call Distribution3",
        "L2_visibility": 1,
        "primary_triage": "Telecom",
        "isMarkham": false,
        "markham_triage": null,
        "Faculty_Support": [
            "Faculty3",
            "Faculty4"
        ]
    },
    {
        "l1_Entity_Nm": "Telephony5",
        "L2_Entity_Id": 8004,
        "L2_Entity_Nm": "Phone",
        "aggregateNm": "Telephony5>Phone",
        "L2_visibility": 1,
        "primary_triage": null,
        "isMarkham": false,
        "markham_triage": null,
        "Faculty_Support": [
            "Faculty1"
        ]
    }
]

我们几乎通过下面的 Jolt 规范获得了所需的输出:

[
  {
    "operation": "shift",
    "spec": {
      "changes": {
        "*": {
          "L2_Entities": {
            "*": {
              "@": "&[&3]",
              "@(2,l1_Entity_Id)": "&[&3].l1_Entity_Id",
              "@(2,l1_Entity_Nm)": "&[&3].l1_Entity_Nm",
              "@(2,l1_visibility)": "&[&3].l1_visibility"
            }
          }
        }
      }
    }
},
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
}
]

但是,它在第二个 json 对象之后引入了一个不需要的 null,如下所示:

[ {
  "L2_Entity_Id" : 8003,
  "L2_Entity_Nm" : "Desktop Software2",
  "visibility" : 1,
  "primary_triage" : "EAAS",
  "markham_triage" : "MK_Team1",
  "Faculty_Support" : [ "Faculty1", "Faculty2", "Faculty4" ],
  "hasReference" : true,
  "l1_Entity_Id" : 7004,
  "l1_Entity_Nm" : "Academic Administration2"
}, {
  "L2_Entity_Id" : 8005,
  "L2_Entity_Nm" : "Automatic Call Distribution3",
  "visibility" : 1,
  "primary_triage" : "Telecom",
  "markham_triage" : null,
  "Faculty_Support" : [ "Faculty3", "Faculty4" ],
  "hasReference" : true,
  "l1_Entity_Id" : 7002,
  "l1_Entity_Nm" : "Telephony5"
}, null, {
  "L2_Entity_Id" : 8004,
  "L2_Entity_Nm" : "Phone",
  "visibility" : 1,
  "primary_triage" : null,
  "markham_triage" : null,
  "Faculty_Support" : [ "Faculty1" ],
  "hasReference" : true,
  "l1_Entity_Id" : 7002,
  "l1_Entity_Nm" : "Telephony5"
} ]

我希望有人能帮助我们解决这个问题。感谢大家给我们的任何帮助。

数组 json null 转换 jolt

评论


答:

1赞 Barbaros Özhan 7/4/2023 #1

您可以更愿意使用 * 符号而不是 @ 符号以及非方括号的 & 符号,例如

[
  {
    "operation": "shift",
    "spec": {
      "changes": {
        "*": {
          "L2_Entities": {
            "*": {
              "*": "&3.&1.&",
              "@2,l1_Entity_Id": "&3.&1.l1_Entity_Id",
              "@2,l1_Entity_Nm": "&3.&1.l1_Entity_Nm",
              "@2,l1_visibility": "&3.&1.l1_visibility"
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

或者,您可以使转换更加动态,而无需单独声明每个属性,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": { // loop through all the "changes"
          "l1*": "&2.&1.Others.&", // accumulate the elements other than "L2_Entities" and "visibility" within an individual object caled "Others"
          "L2_Entities": "&2.&1.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "L2_Entities": {
            "*": {
              "@2,Others": { "*": "&4.&2.&" }, // go two levels up the tree to grab the whole values from the "Others" object
              "*": "&3.&1.&"
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

演示 2

enter image description here

评论

0赞 7/5/2023
非常感谢您对 Özhan @Barbaros投入。您的最后一次震动有效,但它巩固了“可见性”: [ false, 1 ],属性,因为 L1 实体和 L2 实体具有相同的名称。你能帮我把名字L1_Visibility和L2_Visibility分开吗?
0赞 Barbaros Özhan 7/5/2023
欢迎来到 SO @Geek .是的,你是对的,我刚刚修好了。