Home > Software engineering >  How to read specific attribute from the JSON file
How to read specific attribute from the JSON file

Time:10-18

I am facing an issue in reading the json array from the below JSON File. I want to extract the values of "allocations[2]" under paymentGroups array.

I tried the below snippet but it didnt work. data.tenderPlan.tenderPlan.paymentGroups[0].allocations[2].isEligible Please suggest on how to extract all above fields. TIA

{
  "data": {
    "tenderPlan": {
      "__typename": "TenderPlanResponse",
      "tenderPlan": {
        "__typename": "TenderPlan",
        "id": "69d7b860-4b52-11ed-885c-3f3afe777b50",
        "contractId": "96116530-4b51-11ed-8e5d-712e60f5b60e",
        "grandTotal": {
          "__typename": "PriceDetailRow",
          "key": "DOWN_PAYMENT",
          "label": "",
          "displayValue": "$6529.00",
          "value": 6529,
          "info": {
            "__typename": "PriceDetailRowInfo",
            "title": "",
            "message": ""
          }
        },
        "authorizationAmount": {
          "__typename": "PriceDetailRow",
          "key": "DOWN_PAYMENT",
          "label": "",
          "displayValue": "",
          "value": null,
          "info": {
            "__typename": "PriceDetailRowInfo",
            "title": "",
            "message": ""
          }
        },
        "allocationStatus": [],
        "paymentGroups": [
          {
            "__typename": "TenderPlanPaymentGroup",
            "type": "CC",
            "subTotal": {
              "__typename": "PriceDetailRow",
              "key": "DOWN_PAYMENT",
              "label": "",
              "displayValue": "",
              "value": null,
              "info": {
                "__typename": "PriceDetailRowInfo",
                "title": "",
                "message": ""
              }
            },
            "selectedCount": 0,
            "allocations": [],
            "statusMessage": null
          },
          {
            "__typename": "TenderPlanPaymentGroup",
            "type": "OTHER_PAYPAL",
            "subTotal": {
              "__typename": "PriceDetailRow",
              "key": "DOWN_PAYMENT",
              "label": "",
              "displayValue": "",
              "value": null,
              "info": {
                "__typename": "PriceDetailRowInfo",
                "title": "",
                "message": ""
              }
            },
            "selectedCount": 0,
            "allocations": [
              {
                "__typename": "PayPalBaAllocation",
                "paymentHandle": "6aabe4fd-e792-4956-9d88-04567ebed53b",
                "paymentType": "PAYPAL",
                "email": "[email protected]",
                "isSelected": true,
                "lastName": "Doe",
                "firstName": "John"
              }
            ],
            "statusMessage": null
          },
          {
            "__typename": "TenderPlanPaymentGroup",
            "type": "PAYPAL_BA",
            "subTotal": {
              "__typename": "PriceDetailRow",
              "key": "DOWN_PAYMENT",
              "label": "",
              "displayValue": "",
              "value": null,
              "info": {
                "__typename": "PriceDetailRowInfo",
                "title": "",
                "message": ""
              }
            },
            "selectedCount": 0,
            "allocations": [
              {
                "__typename": "PayPalBillingCardAllocation",
                "card": {
                  "__typename": "PayPalBA",
                  "id": "6aabe4fd-e792-4956-9d88-04567ebed53b",
                  "emailAddress": "[email protected]"
                },
                "isEligible": true,
                "allocationAmount": {
                  "__typename": "Price",
                  "displayValue": "$6529.00",
                  "value": 6529
                },
                "paymentType": "PAYPAL",
                "isSelected": true,
                "canEditOrDelete": false,
                "canDeselect": true,
                "statusMessage": null
              }
            ],
            "statusMessage": null
          }
        ],
        "otcDeliveryBenefit": {
          "__typename": "PriceDetailRow",
          "key": "DOWN_PAYMENT",
          "label": "",
          "displayValue": "",
          "value": null,
          "info": {
            "__typename": "PriceDetailRowInfo",
            "title": "",
            "message": ""
          }
        },
        "otherAllowedPayments": [
          {
            "type": "PAYPAL",
            "status": null,
            "isSelected": false
          }
        ],
        "addPaymentType": true,
        "hasAmountUnallocated": false,
        "weightDebitTotal": null,
        "prescriptionDebitTotal": null
      }
    }
  }
}

CodePudding user response:

data.tenderPlan.tenderPlan.paymentGroups[0].allocations is an empty array, so there is no third element in it which you tried to access with .allocations[2].

Read the JSON carefully, ideally in some sort of beautifier - there are many of them available online.

CodePudding user response:

data.tenderPlan.tenderPlan.paymentGroups[0].allocations is empty array in your object.

You can see your all paymentGroups with data.tenderPlan.tenderPlan.paymentGroups to make sure.

data.tenderPlan.tenderPlan.paymentGroups[0].allocations has 0, data.tenderPlan.tenderPlan.paymentGroups[1].allocations has 1 and data.tenderPlan.tenderPlan.paymentGroups[2].allocations has 1 data.

Maybe you are looking for another data.

  • Related