数组的Jolt扁平化引入了不必要的空数组项。

huangapple go评论82阅读模式
英文:

Jolt flattening of array introduces unwanted null array item

问题

我们有一个需求要展平一个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"
} ]
希望有人能帮助我们解决这个问题。感谢您们提供的任何帮助。
英文:

we have a requirement to flatten a JSON object. We have input JSON data that looks like this:

{
"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"
}

Once shifted, we'd like output data like this:

[
{
"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"
]
}
]

We almost got the desired output with the Jolt spec below:

[
{
"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": {
"*": {
"*": "[]"
}
}
}
]

However, it introduces an unwanted null after second json object as shown below:

[ {
"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"
} ]

I hope someone can help us to resolve the issue. Thank you all for any help you might give us.

答案1

得分: 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"
            }
          }
        }
      }
    }
  },
  { // 去掉对象键
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

或者你可以使转换更加动态,无需逐个列出每个属性,如:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "l1*": "&2.&1.Others.&",
          "L2_Entities": "&2.&1.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "L2_Entities": {
            "*": {
              "@2,Others": { "*": "&4.&2.&" },
              "*": "&3.&1.&"
            }
          }
        }
      }
    }
  },
  { // 去掉对象键
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

演示2

数组的Jolt扁平化引入了不必要的空数组项。

英文:

You can rather use * symbol than @ symbol along with non-square-bracketed ampersands such as

[
  {
    "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": {
      "*": {
        "*": ""
      }
    }
  }
]

or you can make the transformation more dynamic in which no need to state each attributes individually such as

[
  {
    "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": {
      "*": {
        "*": ""
      }
    }
  }
]

the demo 2 :

数组的Jolt扁平化引入了不必要的空数组项。

huangapple
  • 本文由 发表于 2023年7月4日 22:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定