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

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Aashish Kumar
  • 1,563
  • 1
  • 15
  • 19

1 Answers1

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.

Aashish Kumar
  • 1,563
  • 1
  • 15
  • 19
  • 1
    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 `==`. – Craig Dec 08 '17 at 17:16
  • 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. – Aashish Kumar Dec 11 '17 at 05:35