gerritvankuipers / aspjson

Classic ASP JSON Class Reading/Writing
78 stars 40 forks source link

Reading sub item / array #29

Open coda-apps opened 11 months ago

coda-apps commented 11 months ago

Hi there.

Thanks for setting up this git.

I have some sample code i am getting from paypal.

I would like to get the 'custom_id' variable (in: purchase_units) I tried custom_id = oJSON.data("purchase_units").item("custom_id") but it was empty. How would i access it? Is it different being within an array?

I also tried this, which works partially - i get an error on what seems to be the 2nd loop of the for loop. But the first loop does seem to access the tag i want. Maybe its throwing an error as this isnt a true array with multiple objects?

   For Each custom_id In oJSON.data("purchase_units")
        Set this = oJSON.data("purchase_units").item(custom_id)
        Response.Write _
        this.item("custom_id")  
    Next

Thanks

JSON BELOW:

{
  "id": "94151283KB577953A",
  "intent": "CAPTURE",
  "status": "COMPLETED",
  "payment_source": {
    "paypal": {
      "email_address": "mo@Test.com",
      "account_id": "A95TCZ88AZQMU",
      "account_status": "UNVERIFIED",
      "name": {
        "given_name": "mo",
        "surname": "Test"
      },
      "address": {
        "country_code": "IL"
      }
    }
  },
  "purchase_units": [{
    "reference_id": "default",
    "amount": {
      "currency_code": "USD",
      "value": "7.50",
      "breakdown": {
        "item_total": {
          "currency_code": "USD",
          "value": "7.50"
        }
      }
    },
    "payee": {
      "email_address": "sb-vuuxy28347827@business.example.com",
      "merchant_id": "C832PPP4ECVRN"
    },
    "description": "Demo Product",
    "custom_id": "DP12345",
    "soft_descriptor": "PAYPAL *TEST STORE",
    "items": [{
      "name": "Demo Product",
      "unit_amount": {
        "currency_code": "USD",
        "value": "7.50"
      },
      "quantity": "1",
      "description": "Demo Product",
      "category": "DIGITAL_GOODS"
    }],
    "shipping": {
      "name": {
        "full_name": "mo Test"
      },
      "address": {
        "address_line_1": "Arba St",
        "admin_area_2": "Tel Aviv",
        "country_code": "IL"
      }
    },
    "payments": {
      "captures": [{
        "id": "8KS14127A2501944D",
        "status": "COMPLETED",
        "amount": {
          "currency_code": "USD",
          "value": "7.50"
        },
        "final_capture": true,
        "seller_protection": {
          "status": "ELIGIBLE",
          "dispute_categories": ["ITEM_NOT_RECEIVED", "UNAUTHORIZED_TRANSACTION"]
        },
        "seller_receivable_breakdown": {
          "gross_amount": {
            "currency_code": "USD",
            "value": "7.50"
          },
          "paypal_fee": {
            "currency_code": "USD",
            "value": "0.43"
          },
          "net_amount": {
            "currency_code": "USD",
            "value": "7.07"
          },
          "receivable_amount": {
            "currency_code": "ILS",
            "value": "25.09"
          },
          "exchange_rate": {
            "source_currency": "USD",
            "target_currency": "ILS",
            "value": "3.5487075"
          }
        },
        "custom_id": "DP12345",
        "links": [{
          "href": "https://api.sandbox.paypal.com/v2/payments/captures/8KS14127A2501944D",
          "rel": "self",
          "method": "GET"
        }, {
          "href": "https://api.sandbox.paypal.com/v2/payments/captures/8KS14127A2501944D/refund",
          "rel": "refund",
          "method": "POST"
        }, {
          "href": "https://api.sandbox.paypal.com/v2/checkout/orders/94151283KB577953A",
          "rel": "up",
          "method": "GET"
        }],
        "create_time": "2023-11-27T21:30:56Z",
        "update_time": "2023-11-27T21:30:56Z"
      }]
    }
  }],
  "payer": {
    "name": {
      "given_name": "mo",
      "surname": "Test"
    },
    "email_address": "mo@Test.com",
    "payer_id": "A95TCZ88AZQMU",
    "address": {
      "country_code": "IL"
    }
  },
  "create_time": "2023-11-27T21:30:42Z",
  "update_time": "2023-11-27T21:30:56Z",
  "links": [{
    "href": "https://api.sandbox.paypal.com/v2/checkout/orders/94151283KB577953A",
    "rel": "self",
    "method": "GET"
  }]
}
Vandroiy78 commented 9 months ago

The attribute "purchase_units" contains an unnamed container, this is probably the place where you have problems. In case you expect only one purchase_unit, you can access the first item directly by index. Otherwise you would have to loop through the collection.

Dim objJSONData : Set objJSONData = objJSON.data
Dim objPurchaseUnits : Set objPurchaseUnits = objJSONData.item("purchase_units")
Dim objPurchaseUnit : Set objPurchaseUnit = objPurchaseUnits.item(0)
MsgBox objPurchaseUnit.item("custom_id")
coda-apps commented 9 months ago

The attribute "purchase_units" contains an unnamed container, this is probably the place where you have problems. In case you expect only one purchase_unit, you can access the first item directly by index. Otherwise you would have to loop through the collection.

Dim objJSONData : Set objJSONData = objJSON.data
Dim objPurchaseUnits : Set objPurchaseUnits = objJSONData.item("purchase_units")
Dim objPurchaseUnit : Set objPurchaseUnit = objPurchaseUnits.item(0)
MsgBox objPurchaseUnit.item("custom_id")

Thank you