How do you retrieve a JSON property from an array in aspJSON?

Viewed 168
testdata =
"{payments:
  [{'status': 'succeeded', 
    'in_store_payment_type': None, 
    'refunds': [], 
    'created_at': '2021-10-20T17:23:41-0400', 
    'amount_paid_display': '53.00', 
    'initial_amount_paid_display': '53.00', 
    'currency': 'usd', 
    'initial_amount_paid': 5300, 
    'pk': 75552720, 
    'type': 'affiliate', 
    'amount_paid': 5300
  }]
}"

I have tried:

<%@ LANGUAGE="VBSCRIPT" %>  
<!--#include file="aspJSON.asp" -->
<%
(read testdata....)

set oJSON= json.parse(testdata)

response.write "Data->" & oJSON.payments.get(0).status
%>

Had hoped to see "succeeded" but no luck.
any suggestions?

1 Answers

A couple of issues with this script, not a fan of the aspJSON library personally but this should get you started (change the #include location to suit your environment).

<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<!-- #include virtual = "/scripts/aspJSON1.19.asp" -->
<%
Dim testdata: testdata = _
  "{""payments"" :" & _
  "  [{""status"": ""succeeded""," & _
  "   ""in_store_payment_type"": None," & _ 
  "   ""refunds"": []," & _
  "   ""created_at"": ""2021-10-20T17:23:41-0400""," & _
  "   ""amount_paid_display"": ""53.00""," & _
  "   ""initial_amount_paid_display"": ""53.00""," & _
  "   ""currency"": ""usd""," & _
  "   ""initial_amount_paid"": 5300," & _
  "   ""pk"": 75552720," & _
  "   ""type"": ""affiliate""," & _
  "   ""amount_paid"": 5300" & _
  " }]" & _
  "}"

Dim oJSON: Set oJSON = New aspJSON
Call oJSON.loadJSON(testdata)
Dim payment
For Each payment In oJSON.data("payments")
  Dim this: Set this = oJSON.data("payments")(payment)
  response.write "Data->" & this.item("status")
Next
%>

Output:

Data->succeeded

A couple of key points

  • JSON needs to use double quotes (") not single quotes (') for it's identifiers. Ref Introducing JSON
  • The aspJSON library does not support the parse() method, instead you use loadJSON().
  • You need to enumerate the payments collection using a For statement to access properties inside the enumerated object.

Useful Links

Related