SOAP text value to Objects

162 views Asked by At

I am using the node package xml-js to convert a SOAP response to json.

The text element response I got in two different requests are detailed below.

How do I change the response in each cases?

1. Response

{
  "declaration": {
    "attributes": {
      "version": "1.0",
      "encoding": "utf-8"
    }
  },
  "elements": [
    {
      "type": "element",
      "name": "Envelope",
      "attributes": {
        "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
      },
      "elements": [
        {
          "type": "element",
          "name": "Body",
          "elements": [
            {
              "type": "element",
              "name": "FetchCustResponse",
              "attributes": {
                "xmlns": "http://DC_API/vproxy/"
              },
              "elements": [
                {
                  "type": "element",
                  "name": "FetchCustResult",
                  "elements": [
                    {
                      "type": "text",
                      "text": "00_AccountNo: 242734005790, AccountType: 1, Address: Cell:340397882, Balance: 0.00, ContactNo: Cell:01039788200, MeterNo: 11178005790, MinAmount: 11,715.40, Name: David James"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Desired output for the text element value

"text" : {
"AccountNo": "04278005790",
"AccountType": "1",
"Address": "08039788217",
"Balance": "0.00",
"ContactNo": "08039788217",
"MeterNo": "04278005790",
"MinAmount": "11,715.40",
"Name": "David James"
}

2.

Response

{
  "declaration": {
    "attributes": {
      "version": "1.0",
      "encoding": "utf-8"
    }
  },
  "elements": [
    {
      "type": "element",
      "name": "Envelope",
      "attributes": {
        "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
      },
      "elements": [
        {
          "type": "element",
          "name": "Body",
          "elements": [
            {
              "type": "element",
              "name": "FetchUsageResponse",
              "attributes": {
                "xmlns": "http://DC_API/vproxy/"
              },
              "elements": [
                {
                  "type": "element",
                  "name": "FetchUsageResult",
                  "elements": [
                    {
                      "type": "text",
                      "text": "00_<NewDataSet>\r\n  <Table>\r\n    <ID>9872686</ID>\r\n    <AccountNo>04278005790</AccountNo>\r\n    <MeterNo>90278005790</MeterNo>\r\n    <AccounType>1</AccounType>\r\n    <PINs>5539</PINs>\r\n    <TotalAmount>4900.00</TotalAmount>\r\n    <CreditedAmount>4802.00</CreditedAmount>\r\n    <FleetComm>9.80</FleetComm>\r\n    <DealerComm>73.50</DealerComm>\r\n    <BankComm>14.70</BankComm>\r\n    <Date>2021-04-24T16:32:36.4+01:00</Date>\r\n    <IDWebTrans>13385836</IDWebTrans>\r\n    <NoOfCards>1</NoOfCards>\r\n    <BUID>12</BUID>\r\n    <TxnReference>BD069419</TxnReference>\r\n    <Token>24559787672457451531</Token>\r\n    <TokeUnit>195.70</TokeUnit>\r\n    <TokenAmt>4558.14</TokenAmt>\r\n    <Charges>341.86</Charges>\r\n  </Table>\r\n  <Table>\r\n    <ID>9763409</ID>\r\n    <AccountNo>04278005790</AccountNo>\r\n    <MeterNo>90278005790</MeterNo>\r\n    <AccounType>1</AccounType>\r\n    <PINs>5539</PINs>\r\n    <TotalAmount>5000.00</TotalAmount>\r\n    <CreditedAmount>4900.00</CreditedAmount>\r\n    <FleetComm>10.00</FleetComm>\r\n    <DealerComm>75.00</DealerComm>\r\n    <BankComm>15.00</BankComm>\r\n    <Date>2021-04-06T12:35:17.367+01:00</Date>\r\n    <IDWebTrans>13270588</IDWebTrans>\r\n    <NoOfCards>1</NoOfCards>\r\n    <BUID>12</BUID>\r\n    <TxnReference>108969129</TxnReference>\r\n    <Token>17218654018179855270</Token>\r\n    <TokeUnit>118.40</TokeUnit>\r\n    <TokenAmt>2520.93</TokenAmt>\r\n    <Charges>2479.07</Charges>\r\n  </Table>\r\n</NewDataSet>"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Desired output for the text element value

Array of Objects

Any pointer, solutions or hints, please?

1

There are 1 answers

0
Adeola Oni On

For the first problem, I wrote a function and it is working so far.

 function filterDown(obj) {
      const obj1 = obj.elements;
      const obj2 = obj1[0];
      const obj3 = obj2.elements;
      const obj4 = obj3[0];
      const obj5 = obj4.elements;
      const obj6 = obj5[0];
      const obj7 = obj6.elements;
      const obj8 = obj7[0];
      const obj9 = obj8.elements;
      const obj10 = obj9[0];
      const obj11 = obj10.text;
      const obj13 = obj11.split("_");
      const obj14 = obj13[1].replace("Cell:", "");
      const obj15 = obj14.replace("Cell:", "");
      const obj16 = obj15.split(":");
      return {
        AccountNo: obj16[1].replace(", AccountType", "").trim(),
        AccountType: obj16[2].replace(", Address", "").trim(),
        Address: obj16[3].replace(", Balance", "").trim(),
        Balance: obj16[4].replace(", ContactNo", "").trim(),
        ContactNo: obj16[5].replace(", MeterNo", "").trim(),
        MeterNo: obj16[6].replace(", MinAmount", "").trim(),
        MinAmount: obj16[7].replace(", Name", "").trim(),
        Name: obj16[8].trim(),
      };
    }
    var desiredResult = filterDown(result);