在JOLT中映射值并循环处理相应的ID – 添加JSON字段值。

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

Mapping values in JOLT and loop for corresponding IDs - adding json field values

问题

我需要转换以下输入JSON,其中ID具有多个ORG_LVL_2代码,应该属于同一个OrgUnits数组,而在我的规范中,会创建一个单独的对象。

之前的输入:

[
  {
    "id": "0000606800",
    "ORG_LVL_2": "Co0053"
  },
  {
    "id": "0000606800",
    "ORG_LVL_2": "Co0054"
  },
  {
    "id": "0000609480",
    "ORG_LVL_2": "Co0055"
  }
]

以下规范:

[ 
  { // 按“id”值分区,同时硬编码为属性“ORG_LVL_1”和“C01”
    "operation": "shift",
    "spec": {
      "*": {
        "id": "@1,id.&",
        "O*": {
          "#ORG_LVL_1|$": "@2,id.&2.orgUnitLevel",
          "#C01|@": "@2,id.&2.orgUnitCode"
        }
      }
    }
  },
  { // 分散独立数组的每个组件到其各自的索引以生成所需的对象
    "operation": "shift",
    "spec": {
      "*": {
        "#test": "IntegrationEntities.integrationEntity[#2].integrationEntityHeader.referenceCodeForEntity",
        "$": ["IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.id",
          "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.glAccountCode"],
        "*": {
          "*": {
            "*": "IntegrationEntities.integrationEntity[#4].integrationEntityDetails.glAccount.orgUnits.orgUnitAssignment[&2].orgUnitDetails[&].&1"
          }
        }
      }
    }
  },
  { // 摆脱冗余生成的空值
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

输入中进行了轻微修改,不是ORG_LVL_2,而是“LVL2”,但我们需要在orgUnits中获得相同的输出:

输入:

[
    {
        "id": "0000606800",
        "status": "Active",
        "name": "Office Supplies",
        "LVL2": "Co0053"
    },
    {
        "id": "0000606800",
        "status": "Active",
        "name": "Office Supplies",
        "LVL2": "Co0054"
    },
    {
        "id": "0000609480",
        "status": "Active",
        "name": "Office Supplies",
        "LVL2": "Co0055"
    }
]

另外,在输出中,除了ID之外,我想显示“name”作为“glAccountDescription”,以及与“glAccountName”相同,并将ID值放入“referenceCodeForEntity”和“integrationTrackingNumber”中,所需的输出如下:

{
    "IntegrationEntities": {
        "integrationEntity": [
            {
                "integrationEntityHeader": {
                    "referenceCodeForEntity": "0000606800",
                    "integrationTrackingNumber": "0000606800"
                },
                "integrationEntityDetails": {
                    "glAccount": {
                        "id": "0000606800",
                        "status": "ACTIVE",
                        "glAccountName": "Office Supplies",
                        "glAccountDescription": "Office Supplies",
                        "glAccountCode": "0000606800",
                        "glAccountTypeId": "GLTYPE001",
                        "orgUnits": {
                            "orgUnitAssignment": [
                                {
                                    "orgUnitDetails": [
                                        {
                                            "orgUnitLevel": "ORG_LVL_1",
                                            "orgUnitCode": "C01"
                                        },
                                        {
                                            "orgUnitLevel": "ORG_LVL_2",
                                            "orgUnitCode": "Co0053"
                                        }
                                    ]
                                },
                                {
                                    "orgUnitDetails": [
                                        {
                                            "orgUnitLevel": "ORG_LVL_1",
                                            "orgUnitCode": "C01"
                                        },
                                        {
                                            "orgUnitLevel": "ORG_LVL_2",
                                            "orgUnitCode": "Co0054"
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}
英文:

I need to transform below Input JSON where the ID has multiple ORG_LVL_2 codes and it should fall under the same OrgUnits array, whereas in my spec a separate object is getting created.

Previous Input:

[
  {
    "id": "0000606800",
    "ORG_LVL_2": "Co0053"
  },
  {
    "id": "0000606800",
    "ORG_LVL_2": "Co0054"
  },
  {
    "id": "0000609480",
    "ORG_LVL_2": "Co0055"
  }
]

Below Spec:

[
  { // partition by "id" values while hardcoding for the attributes "ORG_LVL_1" and "C01"
    "operation": "shift",
    "spec": {
      "*": {
        "id": "@1,id.&",
        "O*": {
          "#ORG_LVL_1|$": "@2,id.&2.orgUnitLevel",
          "#C01|@": "@2,id.&2.orgUnitCode"
        }
      }
    }
  },
  { // dissipate each component of the independent arrays to their respective indexes 
    // in order to generate the desired objects
    "operation": "shift",
    "spec": {
      "*": {
        "#test": "IntegrationEntities.integrationEntity[#2].integrationEntityHeader.referenceCodeForEntity",
        "$": ["IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.id",
"IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.glAccountCode"],
        "*": {
          "*": {
            "*": "IntegrationEntities.integrationEntity[#4].integrationEntityDetails.glAccount.orgUnits.orgUnitAssignment[&2].orgUnitDetails[&].&1"
          }
        }
      }
    }
  },
  { // get rid of redundantly generated null values
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

There's minor modification in the input instead of ORG_LVL_2 its "LVL2" but we need the same output in orgUnits:

Input:

[
    {
        "id": "0000606800",
        "status": "Active",
        "name": "Office Supplies",
        "LVL2": "Co0053"
    },
    {
        "id": "0000606800",
        "status": "Active",
        "name": "Office Supplies",
        "LVL2": "Co0054"
    },
    {
        "id": "0000609480",
        "status": "Active",
        "name": "Office Supplies",
        "LVL2": "Co0055"
    }
]

Additionally, in the output along with the id, I want to display "name" as "glAccountDescription" and same as "glAccountName", and "id" value in "referenceCodeForEntity" and "integrationTrackingNumber" required output as below:

{
    "IntegrationEntities": {
        "integrationEntity": [
            {
                "integrationEntityHeader": {
                    "referenceCodeForEntity": "0000606800",
                    "integrationTrackingNumber" : "0000606800"
                },
                "integrationEntityDetails": {
                    "glAccount": {
                        "id": "0000606800",
                        "status": "ACTIVE",
                        "glAccountName": "Office Supplies",
                        "glAccountDescription": "Office Supplies",
                        "glAccountCode": "0000606800",
                        "glAccountTypeId": "GLTYPE001",
                        "orgUnits": {
                            "orgUnitAssignment": [
                                {
                                    "orgUnitDetails": [
                                        {
                                            "orgUnitLevel": "ORG_LVL_1",
                                            "orgUnitCode": "C01"
                                        },
                                        {
                                            "orgUnitLevel": "ORG_LVL_2",
                                            "orgUnitCode": "Co0053"
                                        }
                                    ]
                                },
                                {
                                    "orgUnitDetails": [
                                        {
                                            "orgUnitLevel": "ORG_LVL_1",
                                            "orgUnitCode": "C01"
                                        },
                                        {
                                            "orgUnitLevel": "ORG_LVL_2",
                                            "orgUnitCode": "Co0054"
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

答案1

得分: 0

I've developed this spec with the help of Barbaros Ozhan, which is currently meeting the expected outcome, if there's a way to minimize code, please feel free to suggest. Thank you.

英文:

I've developed this spec with the help of Barbaros Ozhan, which is currently meeting the expected outcome, if there's way to minimize code, please feel free to suggest. Thank you

[
	{ // generate new attribute, namely "ORG_LVL_2"
		"operation": "modify-overwrite-beta",
		"spec": {
			"*": {
				"ORG_LVL_2": "@(1,LVL2)"
			}
		}
 },
	{ // delete the original attribute
		"operation": "remove",
		"spec": {
			"*": {
				"LVL2": ""
			}
		}
 },
	{ // partition by "id" values while hardcoding for the attributes "ORG_LVL_1" and "C01"
		"operation": "shift",
		"spec": {
			"*": {
				"id": "@1,id.&",
				"name": "@1,id.&",
				"O*": {
					"#ORG_LVL_1|$": "@2,id.&2.orgUnitLevel",
					"#C01|@": "@2,id.&2.orgUnitCode"
				}
			}
		}
    },
	{ // dissipate each component of the independent arrays to their respective indexes 
		// in order to generate the desired objects
		"operation": "shift",
		"spec": {
			"*": {
				"$": [
                    "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.id",
                    "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.glAccountCode",
                    "IntegrationEntities.integrationEntity[#2].integrationEntityHeader.referenceCodeForEntity",
                    "IntegrationEntities.integrationEntity[#2].integrationEntityHeader.integrationTrackingNumber"
                ],
				"#0010": "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.glAccountTypeId",
				"name": [
                    "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.glAccountName",
                    "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.glAccountDescription"
                ],
				"#ACTIVE": "IntegrationEntities.integrationEntity[#2].integrationEntityDetails.glAccount.status",
				"*": {
					"*": {
						"*": "IntegrationEntities.integrationEntity[#4].integrationEntityDetails.glAccount.orgUnits.orgUnitAssignment[&2].orgUnitDetails[&].&1"
					}
				}
			}
		}
    },
	{ // get rid of redundantly generated null values
		"operation": "modify-overwrite-beta",
		"spec": {
			"*": "=recursivelySquashNulls"
		}
},
	{
		"operation": "modify-overwrite-beta",
		"spec": {
			"IntegrationEntities": {
				"integrationEntity": {
					"*": {
						"integrationEntityDetails": {
							"glAccount": {
								"glAccountName": "=firstElement(@(1,&))",
								"glAccountDescription": "=firstElement(@(1,&))"
							}
						}
					}
				}
			}
		}
},
	{
		"operation": "shift",
		"spec": {
			"IntegrationEntities": {
				"integrationEntity": {
					"*": {
						"integrationEntityHeader": {
							"integrationTrackingNumber": "IntegrationEntities.integrationEntity.[&2].integrationEntityHeader.integrationTrackingNumber",
							"referenceCodeForEntity": "IntegrationEntities.integrationEntity.[&2].integrationEntityHeader.referenceCodeForEntity",
							"additionalInfo": "IntegrationEntities.integrationEntity.[&2].integrationEntityHeader.additionalInfo"
						},
						"integrationEntityDetails": {
							"glAccount": {
								"id": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.id",
								"status": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.status",
								"glAccountName": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.glAccountName",
								"glAccountDescription": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.glAccountDescription",
								"glAccountCode": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.glAccountCode",
								"glAccountTypeId": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.glAccountTypeId",
								"parentGlAccountId": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.parentGlAccountId",
								"orgUnits": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.orgUnits",
								"glAccountErpId": "IntegrationEntities.integrationEntity.[&3].integrationEntityDetails.glAccount.glAccountErpId"
							}
						}
					}
				}
			}
		}
}
]

huangapple
  • 本文由 发表于 2023年6月1日 02:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376357.html
匿名

发表评论

匿名网友

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

确定