json - JOLT Transformation: Build array of objects grouping attributes in the same object - Stack Overflow

I am trying to transform JSON using JOLT and need to group StatusReasonCode and StatusReason into the s

I am trying to transform JSON using JOLT and need to group StatusReasonCode and StatusReason into the same object inside a status array. However, my current JOLT spec is incorrectly merging some values into a single array instead of keeping them in separate objects.

Input:

{
  "interfaceInvoiceLines": [
    {
      "LineNumber": 1,
      "interfaceInvoiceLineRejections": [
        {
          "RejectionDescription": "Invalid PO number",
          "TempRejectionDescription": "Invoice line 1 - Invalid PO number"
        },
        {
          "RejectionDescription": "Billed quantity is below zero",
          "TempRejectionDescription": "Invoice line 1 - Billed quantity is below zero"
        }
      ]
    },
    {
      "LineNumber": 2,
      "interfaceInvoiceLineRejections": [
        {
          "RejectionDescription": "Invalid PO number",
          "TempRejectionDescription": "Invoice line 2 - Invalid PO number"
        }
      ]
    }
  ]
}

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "interfaceInvoiceLines": {
        "*": {
          "interfaceInvoiceLineRejections": {
            "*": {
              "RejectionDescription": {
                "Invalid PO number": {
                  "#REF": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReasonCode",
                  "@(2,TempRejectionDescription)": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReason"
                },
                "Billed quantity is below zero": {
                  "#OTH": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReasonCode",
                  "@(2,TempRejectionDescription)": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReason"
                }
              }
            }
          }
        }
      }
    }
  }
]

Actual Output:

{
  "ApplicationResponse": {
    "DocumentResponse": {
      "Response": {
        "status": [
          {
            "StatusReasonCode": [
              "REF",
              "REF"
            ],
            "StatusReason": [
              "Invoice line 1 - Invalid PO number",
              "Invoice line 2 - Invalid PO number"
            ]
          },
          {
            "StatusReasonCode": "OTH",
            "StatusReason": "Invoice line 1 - Billed quantity is below zero"
          }
        ]
      }
    }
  }
}

Expected Output :

{
  "ApplicationResponse": {
    "DocumentResponse": {
      "Response": {
        "status": [
          {
            "StatusReasonCode": "REF",
            "StatusReason": "Invoice line 1 - Invalid PO number"            
          },
          {
            "StatusReasonCode": "OTH",
            "StatusReason": "Invoice line 1 - Billed quantity is below zero"
          },
          {
            "StatusReasonCode": "REF",
            "StatusReason": "Invoice line 2 - Invalid PO number"
          }
        ]
      }
    }
  }
}

I have just started understanding the basics of jolt transformations, but this seems a little complex with the nested structures.

I am trying to transform JSON using JOLT and need to group StatusReasonCode and StatusReason into the same object inside a status array. However, my current JOLT spec is incorrectly merging some values into a single array instead of keeping them in separate objects.

Input:

{
  "interfaceInvoiceLines": [
    {
      "LineNumber": 1,
      "interfaceInvoiceLineRejections": [
        {
          "RejectionDescription": "Invalid PO number",
          "TempRejectionDescription": "Invoice line 1 - Invalid PO number"
        },
        {
          "RejectionDescription": "Billed quantity is below zero",
          "TempRejectionDescription": "Invoice line 1 - Billed quantity is below zero"
        }
      ]
    },
    {
      "LineNumber": 2,
      "interfaceInvoiceLineRejections": [
        {
          "RejectionDescription": "Invalid PO number",
          "TempRejectionDescription": "Invoice line 2 - Invalid PO number"
        }
      ]
    }
  ]
}

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "interfaceInvoiceLines": {
        "*": {
          "interfaceInvoiceLineRejections": {
            "*": {
              "RejectionDescription": {
                "Invalid PO number": {
                  "#REF": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReasonCode",
                  "@(2,TempRejectionDescription)": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReason"
                },
                "Billed quantity is below zero": {
                  "#OTH": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReasonCode",
                  "@(2,TempRejectionDescription)": "ApplicationResponse.DocumentResponse.Response.status[&3].StatusReason"
                }
              }
            }
          }
        }
      }
    }
  }
]

Actual Output:

{
  "ApplicationResponse": {
    "DocumentResponse": {
      "Response": {
        "status": [
          {
            "StatusReasonCode": [
              "REF",
              "REF"
            ],
            "StatusReason": [
              "Invoice line 1 - Invalid PO number",
              "Invoice line 2 - Invalid PO number"
            ]
          },
          {
            "StatusReasonCode": "OTH",
            "StatusReason": "Invoice line 1 - Billed quantity is below zero"
          }
        ]
      }
    }
  }
}

Expected Output :

{
  "ApplicationResponse": {
    "DocumentResponse": {
      "Response": {
        "status": [
          {
            "StatusReasonCode": "REF",
            "StatusReason": "Invoice line 1 - Invalid PO number"            
          },
          {
            "StatusReasonCode": "OTH",
            "StatusReason": "Invoice line 1 - Billed quantity is below zero"
          },
          {
            "StatusReasonCode": "REF",
            "StatusReason": "Invoice line 2 - Invalid PO number"
          }
        ]
      }
    }
  }
}

I have just started understanding the basics of jolt transformations, but this seems a little complex with the nested structures.

Share Improve this question edited Mar 3 at 10:58 Barbaros Özhan 65.6k11 gold badges36 silver badges61 bronze badges asked Mar 3 at 10:20 vinay singhvinay singh 132 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

Need to add one more grouping, eg. convert [&3] to &5_&3(at innermost part) and &3_&1(at 2 levels upper than the innermost) respectively such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "TempRejectionDescription": "&3_&1.StatusReason",
              "RejectionDescription": {
                "Invalid PO number": {
                  "#REF": "&5_&3.StatusReasonCode"
                },
                "Billed quantity is below zero": {
                  "#OTH": "&5_&3.StatusReasonCode"
                }
              }
            }
          }
        }
      }
    }
  },
  { //merge under the desired nesting
    "operation": "shift",
    "spec": {
      "*": "ApplicationResponse.DocumentResponse.Response.status"
    }
  }
]

where the first specification convert to this separated JSON in order to be able easily to manipulate before getting the overall result

{
  "0_0" : {
    "StatusReason" : "Invoice line 1 - Invalid PO number",
    "StatusReasonCode" : "REF"
  },
  "0_1" : {
    "StatusReason" : "Invoice line 1 - Billed quantity is below zero",
    "StatusReasonCode" : "OTH"
  },
  "1_0" : {
    "StatusReason" : "Invoice line 2 - Invalid PO number",
    "StatusReasonCode" : "REF"
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745099231a4611171.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信