Solved

Parsing JSON with VBScript

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

17 Experts available now in Live!

Get 1:1 Help Now