[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parsing JSON with VBScript

Posted on 2013-12-30
6
Medium Priority
?
14,118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 54

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 54

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 1400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 informatio…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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