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

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

Jolt flattening of array introduces unwanted null array item

问题

  1. 我们有一个需求要展平一个JSON对象。我们有以下输入JSON数据:
  2. {
  3. "changes": [
  4. {
  5. "l1_Entity_Id": 7004,
  6. "l1_Entity_Nm": "Academic Administration2",
  7. "visibility": false,
  8. "L2_Entities": [
  9. {
  10. "L2_Entity_Id": 8003,
  11. "L2_Entity_Nm": "Desktop Software2",
  12. "visibility": 1,
  13. "primary_triage": "EAAS",
  14. "markham_triage": "MK_Team1",
  15. "Faculty_Support": [
  16. "Faculty1",
  17. "Faculty2",
  18. "Faculty4"
  19. ],
  20. "hasReference": true
  21. }
  22. ]
  23. },
  24. {
  25. "l1_Entity_Id": 7002,
  26. "l1_Entity_Nm": "Telephony5",
  27. "visibility": false,
  28. "L2_Entities": [
  29. {
  30. "L2_Entity_Id": 8005,
  31. "L2_Entity_Nm": "Automatic Call Distribution3",
  32. "visibility": 1,
  33. "primary_triage": "Telecom",
  34. "markham_triage": null,
  35. "Faculty_Support": [
  36. "Faculty3",
  37. "Faculty4"
  38. ],
  39. "hasReference": true
  40. },
  41. {
  42. "L2_Entity_Id": 8004,
  43. "L2_Entity_Nm": "Phone",
  44. "visibility": 1,
  45. "primary_triage": null,
  46. "markham_triage": null,
  47. "Faculty_Support": [
  48. "Faculty1"
  49. ],
  50. "hasReference": true
  51. }
  52. ]
  53. }
  54. ],
  55. "methodName": "generateEntitiesChgReport"
  56. }
  57. 展开后,我们想要的输出数据如下:
  58. [
  59. {
  60. "l1_Entity_Nm": "Academic Administration2",
  61. "L1_visibility": false,
  62. "L2_Entity_Id": 8003,
  63. "L2_Entity_Nm": "Desktop Software2",
  64. "aggregateNm": "Academic Administration2>Desktop Software2",
  65. "L2_visibility": 1,
  66. "primary_triage": "EAAS",
  67. "isMarkham": true,
  68. "markham_triage": "MK_Team1",
  69. "Faculty_Support": [
  70. "Faculty1",
  71. "Faculty2",
  72. "Faculty4"
  73. ]
  74. },
  75. {
  76. "l1_Entity_Nm": "Telephony5",
  77. "L1_visibility": false,
  78. "L2_Entity_Id": 8005,
  79. "L2_Entity_Nm": "Automatic Call Distribution3",
  80. "aggregateNm": "Telephony5>Automatic Call Distribution3",
  81. "L2_visibility": 1,
  82. "primary_triage": "Telecom",
  83. "isMarkham": false,
  84. "markham_triage": null,
  85. "Faculty_Support": [
  86. "Faculty3",
  87. "Faculty4"
  88. ]
  89. },
  90. {
  91. "l1_Entity_Nm": "Telephony5",
  92. "L2_Entity_Id": 8004,
  93. "L2_Entity_Nm": "Phone",
  94. "aggregateNm": "Telephony5>Phone",
  95. "L2_visibility": 1,
  96. "primary_triage": null,
  97. "isMarkham": false,
  98. "markham_triage": null,
  99. "Faculty_Support": [
  100. "Faculty1"
  101. ]
  102. }
  103. ]
  104. 我们几乎用以下Jolt规范得到了想要的输出:
  105. [
  106. {
  107. "operation": "shift",
  108. "spec": {
  109. "changes": {
  110. "*": {
  111. "L2_Entities": {
  112. "*": {
  113. "@": "&[&3]",
  114. "@(2,l1_Entity_Id)": "&[&3].l1_Entity_Id",
  115. "@(2,l1_Entity_Nm)": "&[&3].l1_Entity_Nm",
  116. "@(2,l1_visibility)": "&[&3].l1_visibility"
  117. }
  118. }
  119. }
  120. }
  121. }
  122. },
  123. {
  124. "operation": "shift",
  125. "spec": {
  126. "*": {
  127. "*": "[]"
  128. }
  129. }
  130. }
  131. ]
  132. 然而,它在第二个JSON对象之后引入了一个不需要的null,如下所示:
  133. [ {
  134. "L2_Entity_Id" : 8003,
  135. "L2_Entity_Nm" : "Desktop Software2",
  136. "visibility" : 1,
  137. "primary_triage" : "EAAS",
  138. "markham_triage" : "MK_Team1",
  139. "Faculty_Support" : [ "Faculty1", "Faculty2", "Faculty4" ],
  140. "hasReference" : true,
  141. "l1_Entity_Id" : 7004,
  142. "l1_Entity_Nm" : "Academic Administration2"
  143. }, {
  144. "L2_Entity_Id" : 8005,
  145. "L2_Entity_Nm" : "Automatic Call Distribution3",
  146. "visibility" : 1,
  147. "primary_triage" : "Telecom",
  148. "markham_triage" : null,
  149. "Faculty_Support" : [ "Faculty3", "Faculty4" ],
  150. "hasReference" : true,
  151. "l1_Entity_Id" : 7002,
  152. "l1_Entity_Nm" : "Telephony5"
  153. }, null, {
  154. "L2_Entity_Id" : 8004,
  155. "L2_Entity_Nm" : "Phone",
  156. "visibility" : 1,
  157. "primary_triage" : null,
  158. "markham_triage" : null,
  159. "Faculty_Support" : [ "Faculty1" ],
  160. "hasReference" : true,
  161. "l1_Entity_Id" : 7002,
  162. "l1_Entity_Nm" : "Telephony5"
  163. } ]
  164. 希望有人能帮助我们解决这个问题。感谢您们提供的任何帮助。
英文:

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

  1. {
  2. "changes": [
  3. {
  4. "l1_Entity_Id": 7004,
  5. "l1_Entity_Nm": "Academic Administration2",
  6. "visibility": false,
  7. "L2_Entities": [
  8. {
  9. "L2_Entity_Id": 8003,
  10. "L2_Entity_Nm": "Desktop Software2",
  11. "visibility": 1,
  12. "primary_triage": "EAAS",
  13. "markham_triage": "MK_Team1",
  14. "Faculty_Support": [
  15. "Faculty1",
  16. "Faculty2",
  17. "Faculty4"
  18. ],
  19. "hasReference": true
  20. }
  21. ]
  22. },
  23. {
  24. "l1_Entity_Id": 7002,
  25. "l1_Entity_Nm": "Telephony5",
  26. "visibility": false,
  27. "L2_Entities": [
  28. {
  29. "L2_Entity_Id": 8005,
  30. "L2_Entity_Nm": "Automatic Call Distribution3",
  31. "visibility": 1,
  32. "primary_triage": "Telecom",
  33. "markham_triage": null,
  34. "Faculty_Support": [
  35. "Faculty3",
  36. "Faculty4"
  37. ],
  38. "hasReference": true
  39. },
  40. {
  41. "L2_Entity_Id": 8004,
  42. "L2_Entity_Nm": "Phone",
  43. "visibility": 1,
  44. "primary_triage": null,
  45. "markham_triage": null,
  46. "Faculty_Support": [
  47. "Faculty1"
  48. ],
  49. "hasReference": true
  50. }
  51. ]
  52. }
  53. ],
  54. "methodName": "generateEntitiesChgReport"
  55. }

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

  1. [
  2. {
  3. "l1_Entity_Nm": "Academic Administration2",
  4. "L1_visibility": false,
  5. "L2_Entity_Id": 8003,
  6. "L2_Entity_Nm": "Desktop Software2",
  7. "aggregateNm": "Academic Administration2>Desktop Software2",
  8. "L2_visibility": 1,
  9. "primary_triage": "EAAS",
  10. "isMarkham": true,
  11. "markham_triage": "MK_Team1",
  12. "Faculty_Support": [
  13. "Faculty1",
  14. "Faculty2",
  15. "Faculty4"
  16. ]
  17. },
  18. {
  19. "l1_Entity_Nm": "Telephony5",
  20. "L1_visibility": false,
  21. "L2_Entity_Id": 8005,
  22. "L2_Entity_Nm": "Automatic Call Distribution3",
  23. "aggregateNm": "Telephony5>Automatic Call Distribution3",
  24. "L2_visibility": 1,
  25. "primary_triage": "Telecom",
  26. "isMarkham": false,
  27. "markham_triage": null,
  28. "Faculty_Support": [
  29. "Faculty3",
  30. "Faculty4"
  31. ]
  32. },
  33. {
  34. "l1_Entity_Nm": "Telephony5",
  35. "L2_Entity_Id": 8004,
  36. "L2_Entity_Nm": "Phone",
  37. "aggregateNm": "Telephony5>Phone",
  38. "L2_visibility": 1,
  39. "primary_triage": null,
  40. "isMarkham": false,
  41. "markham_triage": null,
  42. "Faculty_Support": [
  43. "Faculty1"
  44. ]
  45. }
  46. ]

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "changes": {
  6. "*": {
  7. "L2_Entities": {
  8. "*": {
  9. "@": "&[&3]",
  10. "@(2,l1_Entity_Id)": "&[&3].l1_Entity_Id",
  11. "@(2,l1_Entity_Nm)": "&[&3].l1_Entity_Nm",
  12. "@(2,l1_visibility)": "&[&3].l1_visibility"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. },
  19. {
  20. "operation": "shift",
  21. "spec": {
  22. "*": {
  23. "*": "[]"
  24. }
  25. }
  26. }
  27. ]

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

  1. [ {
  2. "L2_Entity_Id" : 8003,
  3. "L2_Entity_Nm" : "Desktop Software2",
  4. "visibility" : 1,
  5. "primary_triage" : "EAAS",
  6. "markham_triage" : "MK_Team1",
  7. "Faculty_Support" : [ "Faculty1", "Faculty2", "Faculty4" ],
  8. "hasReference" : true,
  9. "l1_Entity_Id" : 7004,
  10. "l1_Entity_Nm" : "Academic Administration2"
  11. }, {
  12. "L2_Entity_Id" : 8005,
  13. "L2_Entity_Nm" : "Automatic Call Distribution3",
  14. "visibility" : 1,
  15. "primary_triage" : "Telecom",
  16. "markham_triage" : null,
  17. "Faculty_Support" : [ "Faculty3", "Faculty4" ],
  18. "hasReference" : true,
  19. "l1_Entity_Id" : 7002,
  20. "l1_Entity_Nm" : "Telephony5"
  21. }, null, {
  22. "L2_Entity_Id" : 8004,
  23. "L2_Entity_Nm" : "Phone",
  24. "visibility" : 1,
  25. "primary_triage" : null,
  26. "markham_triage" : null,
  27. "Faculty_Support" : [ "Faculty1" ],
  28. "hasReference" : true,
  29. "l1_Entity_Id" : 7002,
  30. "l1_Entity_Nm" : "Telephony5"
  31. } ]

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

答案1

得分: 1

你可以使用 * 符号而不是 @ 符号,以及与非方括号的和号一起使用,例如:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "changes": {
  6. "*": {
  7. "L2_Entities": {
  8. "*": {
  9. "*": "&3.&1.&",
  10. "@2,l1_Entity_Id": "&3.&1.l1_Entity_Id",
  11. "@2,l1_Entity_Nm": "&3.&1.l1_Entity_Nm",
  12. "@2,l1_visibility": "&3.&1.l1_visibility"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. },
  19. { // 去掉对象键
  20. "operation": "shift",
  21. "spec": {
  22. "*": {
  23. "*": ""
  24. }
  25. }
  26. }
  27. ]

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "*": {
  7. "l1*": "&2.&1.Others.&",
  8. "L2_Entities": "&2.&1.&"
  9. }
  10. }
  11. }
  12. },
  13. {
  14. "operation": "shift",
  15. "spec": {
  16. "*": {
  17. "*": {
  18. "L2_Entities": {
  19. "*": {
  20. "@2,Others": { "*": "&4.&2.&" },
  21. "*": "&3.&1.&"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. },
  28. { // 去掉对象键
  29. "operation": "shift",
  30. "spec": {
  31. "*": {
  32. "*": ""
  33. }
  34. }
  35. }
  36. ]

演示2

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

英文:

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "changes": {
  6. "*": {
  7. "L2_Entities": {
  8. "*": {
  9. "*": "&3.&1.&",
  10. "@2,l1_Entity_Id": "&3.&1.l1_Entity_Id",
  11. "@2,l1_Entity_Nm": "&3.&1.l1_Entity_Nm",
  12. "@2,l1_visibility": "&3.&1.l1_visibility"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. },
  19. { // get rid of the object keys
  20. "operation": "shift",
  21. "spec": {
  22. "*": {
  23. "*": ""
  24. }
  25. }
  26. }
  27. ]

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "*": { // loop through all the "changes"
  7. "l1*": "&2.&1.Others.&", // accumulate the elements other than "L2_Entities" and "visibility" within an individual object caled "Others"
  8. "L2_Entities": "&2.&1.&"
  9. }
  10. }
  11. }
  12. },
  13. {
  14. "operation": "shift",
  15. "spec": {
  16. "*": {
  17. "*": {
  18. "L2_Entities": {
  19. "*": {
  20. "@2,Others": { "*": "&4.&2.&" }, // go two levels up the tree to grab the whole values from the "Others" object
  21. "*": "&3.&1.&"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. },
  28. { // get rid of the object keys
  29. "operation": "shift",
  30. "spec": {
  31. "*": {
  32. "*": ""
  33. }
  34. }
  35. }
  36. ]

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:

确定