Avatar of slegy
slegy
 asked on

Parsing JSON with VBScript

A response received from an API call is formatted as follows:

{
  "response": {
    "request": {
      "date": "Thu, 25 Aug 2011 16:31:16  0200",
      "resource": "/connections.json",
      "status": {
        "flag": "success",
        "code": 200
      }
    },
    "result": {
      "status": {
        "flag": "success",
        "count": 2
      },
      "data": {
        "connections": [
          {
            "connection": {
              "date": "Tue, 23 Aug 2011 14:12:01  0200",
              "plugin": "social_login",
              "token": "7a559a39-51a5-4f21-92aa-cc880da2233f"
            }
          },
          {
            "connection": {
              "date": "Tue, 23 Aug 2011 14:15:01  0200",
              "plugin": "social_login",
              "token": "6430aaf1-e8fd-41d9-b3e1-abae114f68b2"
            }
          }
        ]
      }
    }
  }
}

Open in new window


I need to parse it and log some of the returned fields. Despite all my research, I haven't found the right example to accomplish this. Any help would be greatly appreciated.
VB ScriptASP

Avatar of undefined
Last Comment
Big Monty

8/22/2022 - Mon
Scott Fell

I have just used jquery for this and posted to an asp page.  

I found http://www.aspjson.com/
Looking at the sample you just loop through and take what you need.  If you have a lot of data to parse, it will go faster doing this client side.  If it is just a small amount of data, this will work fine.
Set oJSON = New aspJSON

'Load JSON string
oJSON.loadJSON(jsonstring)

'Get single value
Response.Write oJSON.data("firstName") & "<br>"

'Loop through collection
For Each phonenr In oJSON.data("phoneNumber")
    Set this = oJSON.data("phoneNumber").item(phonenr)
    Response.Write _
    this.item("type") & ": " & _
    this.item("number") & "<br>"
Next

'Update/Add value
oJSON.data("firstName") = "James"

'Return the object
Response.Write oJSON.JSONoutput()

Open in new window


Which fields do you want?
slegy

ASKER
I stumbled across that example several times and don't really understand it. As I recall, phoneNumber is an array, and the code doesn't reference other items on different levels. What I am most interested in (in my example) is date and status.  I just haven't been successful in finding the correct way to reference the different levels of the tree.
Scott Fell

What needs to be done with the data?  and how are you accessing it?  

I think it would be faster and easier to use jquery on the client side and if you need to post anything to the db, we can use ajax. If you just need to display the data, that is even easier.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
slegy

ASKER
What I most need to do is test flag and.code under response.request.status. But ideally, I want to understand how to extract the field values in VBScript so I can access any field I need to.
ASKER CERTIFIED SOLUTION
Big Monty

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.