0

I am using Postman to access a legacy API and I get a response as an XML for this API.

I am able to parse it as a JSON using the below code:

var response = xml2Json(responseBody);

and able to access the id in the transaction object like this

console.log("Transaction Id: " + response.transaction.id);

from the XML below:

<?xml version="1.0" encoding="UTF-8"?>
<transaction>
    <id>n13cmyf9</id>
    <status>authorized</status>
    <type>sale</type>
    <currency-iso-code>USD</currency-iso-code>
    <amount>100.00</amount>
    <amount-requested>100.00</amount-requested>
    <merchant-account-id>braintreedemo_USD</merchant-account-id>
    <sub-merchant-account-id nil="true"/>
    <master-merchant-account-id nil="true"/>
    <order-id nil="true"/>
    <created-at type="datetime">2022-05-03T04:59:19Z</created-at>
    <updated-at type="datetime">2022-05-03T04:59:20Z</updated-at>
    <customer>
        <id>12345</id>
        <first-name>Eric</first-name>
        <last-name>Smith</last-name>
        <company></company>
        <email>eric.nelson+kounttesting@braintreepayments.com</email>
        <website></website>
        <phone>3127891234</phone>
        <fax></fax>
        <global-id>Y3VzdG9tZXJfMTIzNDU</global-id>
    </customer>

But unable to access api-error-response.transaction.id in the below XML. Throws undefined object error. Tried from this post that variables with - has to be accessed with the quotes, but still the same undefined object error.

Tried the below but none worked

console.log(["response.api-error-response.api-error-response.params.transaction.id"]);
console.log(["response.api-error-response.api-error-response[0].params.transaction.id"]);
console.log(["response.api-error-response.api-error-response.transaction.errors[0].error.code"]);

Any pointers would be greatly helpful.

<?xml version="1.0" encoding="UTF-8"?>
<api-error-response>
    <errors>
        <errors type="array"/></errors>
    <params>
        <transaction>
            <type>sale</type>
            <amount>100.00</amount>
            <payment-method-nonce>2577ca3b-2f0d-0713-7e53-44c8153be36a</payment-method-nonce>
            <options>
                <store-in-vault-on-success>true</store-in-vault-on-success>
            </options>
        </transaction>
    </params>
    <message>Gateway Rejected: risk_threshold</message>
    <transaction>
        <id>256x3585</id>
        <status>gateway_rejected</status>
        <type>sale</type>
        <currency-iso-code>USD</currency-iso-code>
        <amount>100.00</amount>
        <amount-requested>100.00</amount-requested>
        <merchant-account-id>braintreedemo_USD</merchant-account-id>
        <sub-merchant-account-id nil="true"/>
        <master-merchant-account-id nil="true"/>
        <order-id nil="true"/>
        <created-at type="datetime">2022-05-03T02:40:29Z</created-at>
        <updated-at type="datetime">2022-05-03T02:40:29Z</updated-at>
        <customer>
            <id>12345</id>
            <first-name>Eric</first-name>
            <last-name>Smith</last-name>
            <company></company>
            <email>eric.nelson+kounttesting@braintreepayments.com</email>
            <website></website>
            <phone>3127891234</phone>
            <fax></fax>
            <global-id>Y3VzdG9tZXJfMTIzNDU</global-id>
        </customer>
Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
oneworld
  • 739
  • 4
  • 17
  • 29
  • Instead of posting the XML you're not even using for anything, post the JSON that you're trying to access. Your question will probably become clear to you just from looking at the JSON structure. – Tomalak May 03 '22 at 06:01

0 Answers0