Solved

Parsing JSON with VBScript

Posted on 2013-12-30
6
10,888 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 32

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now