2

Actually I want to get value from a JSON with Dynamic key in VBScript. I try to find similar question if any body asked already but nothing find for VBScript.

So below is a sample json:

{
    "assessmenttype": [{
        "id": "129666",
        "formattedvalue": "wT",
        "value": "WT"
    }],
    "jobid": "2017-2752",
    "jobtitle": "XYZ",
    "links": [{
        "rel": "self",
        "title": "The current profile being viewed.",
        "url": "https://dummyUrl.com/customers"
    }],
    "field33005": {
        "id": "C121",
        "formattedvalue": "XYZ",
        "value": "XYZ"
    }
}

So in above JSON(which is client specific), as for one client node name is field33005 but for any other client this field name might be field38045 and so on.. so the challenge is to get the value of "value" sub field in this field33005 custom field.

please help me as I am not professional in JSON Parsing with VBScript.

Note: For json parsing I am using json2-min.js library

5
  • VBScript doesn't come with a JSON parser. There's the VbsJson class, but you're probably better off using a language that actually comes with a JSON parser, like JScript or PowerShell. Commented Dec 5, 2017 at 8:55
  • For json parsing I am using json2-min.js library. Commented Dec 5, 2017 at 9:16
  • @AnsgarWiechers any help? Commented Dec 5, 2017 at 9:44
  • 1
    If you're using ASP please say so. You can do things in ASP that you cannot do in plain VBScript. Commented Dec 5, 2017 at 10:45
  • Okay sorry for confusion and being not so clear in my question. Commented Dec 5, 2017 at 10:46

1 Answer 1

2

To answer my own question I make one function in JavaScript as we can call a js function from VBSript in ASP.

<script runat="server" language="javascript">

  function getJSONObject(targetJSONObject, propName)
  {

    for (var prop in targetJSONObject)
    {
      if (prop = propName)
      {
        return targetJSONObject[prop].value;
      }
    }
  return "";
  }
</script>

In the above method we need to pass the actual Json and name of custom field then it will return the "value" sub node of that custom field.

Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps I misunderstood your question, but this doesn't answer it. I thought the problem was that you didn't know what the property name was but had to get the value regardless. This function requires that you know the property name. Also, the test for equality in the if statement should be ==.
Actually the property name would be different for every customer but we should know in advance what would be the name of property for a specific customer and we can maintain this property name either in DB or some customer specific file. So I had to write a generic code by which I could get the value of that customer specific property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.