Skip to content

Instantly share code, notes, and snippets.

@mikeacjones
Last active January 6, 2021 16:47
Show Gist options
  • Save mikeacjones/1c01721df3ca4558d76643a9401ab8dd to your computer and use it in GitHub Desktop.
Save mikeacjones/1c01721df3ca4558d76643a9401ab8dd to your computer and use it in GitHub Desktop.
Function to convert XML to JSON, while expanding attribute if there are any
%dw 2.0
output application/json
var expandXmlAttrs = (payload) ->
payload match {
case is Array -> payload map expandXmlAttrs($)
case is Object -> payload mapObject {
(($$): expandXmlAttrs($)) if ($$.@ == null),
(($$): {
value: expandXmlAttrs($),
($.@)
}) if ($$.@ != null)
}
else -> payload
}
---
expandXmlAttrs(payload)
@mikeacjones
Copy link
Author

mikeacjones commented Jan 6, 2021

The function allows easy expansion of XML to JSON while preserving attributes. Currently, if there are no attrs, value is just passed as key. If there are attributes, value is passed as object with value key and attributes expanding into this object.

Example input:

<EDPPPersonSrch>
   <SrchMsgRqHdr>
    <jXchangeHdr>
     <JxVer>2020</JxVer>
     <AuditUsrId>jxchange</AuditUsrId>
     <AuditWsId>ThirdParty</AuditWsId>
     <ConsumerName>jxchange</ConsumerName>
     <ConsumerProd>soatest</ConsumerProd>
     <Ver_1/>
     <jXLogTrackingId>456</jXLogTrackingId>
     <Ver_2/>
     <InstRtId JHANull="" Rstr="">123456780</InstRtId>
     <InstEnv>Prod</InstEnv>
     <Ver_3/>
     <Ver_4/>
     <Ver_5/>
     <ValidConsmName>ImageCenter Admin</ValidConsmName>
     <ValidConsmProd>ImageCenter</ValidConsmProd>
     <Ver_6/>
    </jXchangeHdr>
    <MaxRec>2</MaxRec>
    <Cursor>2</Cursor>
    <Ver_1/>
    <Ver_2/>
    <Ver_3/>
   </SrchMsgRqHdr>
   <ProdCode JHANull="" Rstr="">jha-imagecenter</ProdCode>
   <LastName JHANull="" Rstr="" SrchType="">Jones</LastName>
   <AcctId JHANull="" Rstr="" SrchType="Positive">70023888848</AcctId>
   <AcctType JHANull="" Rstr="">Savings</AcctType>
   <Custom/>
   <Ver_1/>
  </EDPPPersonSrch>

Example output:

{
  "EDPPPersonSrch": {
    "SrchMsgRqHdr": {
      "jXchangeHdr": {
        "JxVer": "2020",
        "AuditUsrId": "jxchange",
        "AuditWsId": "ThirdParty",
        "ConsumerName": "jxchange",
        "ConsumerProd": "soatest",
        "Ver_1": null,
        "jXLogTrackingId": "456",
        "Ver_2": null,
        "InstRtId": {
          "value": "123456780",
          "JHANull": "",
          "Rstr": ""
        },
        "InstEnv": "Prod",
        "Ver_3": null,
        "Ver_4": null,
        "Ver_5": null,
        "ValidConsmName": "ImageCenter Admin",
        "ValidConsmProd": "ImageCenter",
        "Ver_6": null
      },
      "MaxRec": "2",
      "Cursor": "2",
      "Ver_1": null,
      "Ver_2": null,
      "Ver_3": null
    },
    "ProdCode": {
      "value": "jha-imagecenter",
      "JHANull": "",
      "Rstr": ""
    },
    "LastName": {
      "value": "Jones",
      "JHANull": "",
      "Rstr": "",
      "SrchType": ""
    },
    "AcctId": {
      "value": "70023888848",
      "JHANull": "",
      "Rstr": "",
      "SrchType": "Positive"
    },
    "AcctType": {
      "value": "Savings",
      "JHANull": "",
      "Rstr": ""
    },
    "Custom": null,
    "Ver_1": null
  }
}

For a more consistent, but larger data output, consider changing the function to something like this:

%dw 2.0
output application/json

var expandXmlAttrs = (payload) ->
    payload match {
        case is Array -> payload map expandXmlAttrs($)
        case is Object -> payload mapObject {
            ($$): {
                value: expandXmlAttrs($),
                attributes: ($.@) pluck { key: $$, value: $ }
            }
        }
        else -> payload
    }

---
expandXmlAttrs(payload)

Which means all keys will have an object with a value key, and potentially attribute keys.

Same input, different output:

{
  "EDPPPersonSrch": {
    "value": {
      "SrchMsgRqHdr": {
        "value": {
          "jXchangeHdr": {
            "value": {
              "JxVer": {
                "value": "2020",
                "attributes": null
              },
              "AuditUsrId": {
                "value": "jxchange",
                "attributes": null
              },
              "AuditWsId": {
                "value": "ThirdParty",
                "attributes": null
              },
              "ConsumerName": {
                "value": "jxchange",
                "attributes": null
              },
              "ConsumerProd": {
                "value": "soatest",
                "attributes": null
              },
              "Ver_1": {
                "value": null,
                "attributes": null
              },
              "jXLogTrackingId": {
                "value": "456",
                "attributes": null
              },
              "Ver_2": {
                "value": null,
                "attributes": null
              },
              "InstRtId": {
                "value": "123456780",
                "attributes": [
                  {
                    "key": "JHANull",
                    "value": ""
                  },
                  {
                    "key": "Rstr",
                    "value": ""
                  }
                ]
              },
              "InstEnv": {
                "value": "Prod",
                "attributes": null
              },
              "Ver_3": {
                "value": null,
                "attributes": null
              },
              "Ver_4": {
                "value": null,
                "attributes": null
              },
              "Ver_5": {
                "value": null,
                "attributes": null
              },
              "ValidConsmName": {
                "value": "ImageCenter Admin",
                "attributes": null
              },
              "ValidConsmProd": {
                "value": "ImageCenter",
                "attributes": null
              },
              "Ver_6": {
                "value": null,
                "attributes": null
              }
            },
            "attributes": null
          },
          "MaxRec": {
            "value": "2",
            "attributes": null
          },
          "Cursor": {
            "value": "2",
            "attributes": null
          },
          "Ver_1": {
            "value": null,
            "attributes": null
          },
          "Ver_2": {
            "value": null,
            "attributes": null
          },
          "Ver_3": {
            "value": null,
            "attributes": null
          }
        },
        "attributes": null
      },
      "ProdCode": {
        "value": "jha-imagecenter",
        "attributes": [
          {
            "key": "JHANull",
            "value": ""
          },
          {
            "key": "Rstr",
            "value": ""
          }
        ]
      },
      "LastName": {
        "value": "Jones",
        "attributes": [
          {
            "key": "JHANull",
            "value": ""
          },
          {
            "key": "Rstr",
            "value": ""
          },
          {
            "key": "SrchType",
            "value": ""
          }
        ]
      },
      "AcctId": {
        "value": "70023888848",
        "attributes": [
          {
            "key": "JHANull",
            "value": ""
          },
          {
            "key": "Rstr",
            "value": ""
          },
          {
            "key": "SrchType",
            "value": "Positive"
          }
        ]
      },
      "AcctType": {
        "value": "Savings",
        "attributes": [
          {
            "key": "JHANull",
            "value": ""
          },
          {
            "key": "Rstr",
            "value": ""
          }
        ]
      },
      "Custom": {
        "value": null,
        "attributes": null
      },
      "Ver_1": {
        "value": null,
        "attributes": null
      }
    },
    "attributes": null
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment