Solved

Parsing JSON with VBScript

Posted on 2013-12-30
6
12,010 Views
Last Modified: 2014-01-02
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.
0
Comment
Question by:slegy
  • 2
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39747208
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?
0
 

Author Comment

by:slegy
ID: 39747251
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.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39747279
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.
0
 

Author Comment

by:slegy
ID: 39747388
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.
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 350 total points
ID: 39748369
using the aspjson plugin that Scott mentions, all you would need to do to get the data you want is include the necessary files, then do:

    'assuming strJSONData is the data you receive from the API call
    data = JSON.parse( join( array( strJSONData ) ) )
    theDate = data.response.request.date
    resource = data.response.request.resource
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question