Home > Blockchain >  How to Parse nested Json In Java script
How to Parse nested Json In Java script

Time:11-28

I have this json in js, and i am trying to get the key from the "Data" Object, like the SessionId and Email.

Please Help...

{
  "HasError": false,
  "Errors": [],
  "ObjectName": "WebCheckoutCreateSessionResponse",
  "Data": {
    "HasError": false,
    "ReturnCode": 0,
    "ReturnMessage": null,
    "SessionId": "abcde",
    "SessionUrl": "https://pci.aaa.com/WebCheckout/Angular/Checkout.html#!/ZCreditWebCheckout/55cf7d1306b2bcc15d791dde524d2b4f616421172e6d75a013597c8dd2668843",
    "RequestData": {
      "Key": "ad",
      "Local": "He",
      "UniqueId": "<InvNo>1705</InvNo><InvYear>3102</InvYear><SugMismah>15</SugMismah><key>ad</key>",
      "SuccessUrl": "",
      "CancelUrl": "",
      "CallbackUrl": "",
      "PaymentType": "regular",
      "CreateInvoice": false,
      "AdditionalText": "",
      "ShowCart": true,
      "ThemeColor": "005ebb",
      "Installments": {
        "Type": "regular",
        "MinQuantity": 1,
        "MaxQuantity": 12
      },
      "Customer": {
        "Email": "[email protected]",
        "Name": "Demo Client",
        "PhoneNumber": "077-3233190",
        "Attributes": {
          "HolderId": "none",
          "Name": "required",
          "PhoneNumber": "required",
          "Email": "optional"
        }
      },
      "CartItems": [
        {
          "Amount": 117,
          "Currency": "US",
          "Name": "Danny ",
          "Description": "Hi ",
          "Quantity": 1,
          "Image": "https://www.aaa.com/site/wp-content/themes/z-credit/img/decisions/decision2.png",
          "IsTaxFree": false
        }
      ],
      "GetCurrencyCode": "376",
      "BitButtonEnabled": true,
      "MaxPayButtonEnabled": true,
      "ApplePayButtonEnabled": true,
      "GooglePayButtonEnabled": true,
      "ShowTotalSumInPayButton": true
    }
  },
  "ResponseType": 0
}
const population = JSON.parse(xhr.responseText);
      for (const key in population) {
        if (population.hasOwnProperty(key)) {
          console.log(`${key}: ${population[key]}`);
        }
      }

CodePudding user response:

You forgot to index the Data property.

const population = JSON.parse(xhr.responseText).Data;

for (const key in population) {
  console.log(`${key}: ${population[key]}`);
}
  • Related