1

I'm using ASPJSON (https://www.aspjson.com/) to write JSON in Classic ASP / VBScript. I'm then attempting to loop through the JSON and access both the key and value. As it stands I can only access the value.

JSON

Set InitialGlaDataJsonForm = New aspJSON
With InitialGlaDataJsonForm.data

    .Add "glaFormContent", InitialGlaDataJsonForm.Collection() 
    With .item("glaFormContent")

        .Add "form", "myform"
        .Add "email", "email@address"
        .Add "phone", "012345"
        .Add "name", "Joe Bloggs"

    End With

End With

JSON Looks like this

{
    "glaFormContent": {
        "form": "myform",
        "email": "email@address",
        "phone": "012345",
        "name": "Joe Bloggs"
    }
}

Looping through the JSON, accessing the value

For Each element In InitialGlaDataJsonForm.data("glaFormContent")
     response.write InitialGlaDataJsonForm.data("glaFormContent").item(element) & "<br>"
Next

The above returns the value of during each iteration, however I need to be able to get hold of the key, without writing it out.

So my question is how do I access the key in the same manner as the value?

Many thanks

user692942
  • 16,398
  • 7
  • 76
  • 175
Boomfelled
  • 515
  • 5
  • 14
  • 1
    `Response.Write element` will give you the key. In ASPJSON when looping through the `data` dictionary, `element` is your key and `.item(element)` gives you the value related to that key. – user692942 Nov 25 '22 at 16:10
  • 1
    Related - [How do you retrieve a JSON property from an array in aspJSON?](https://stackoverflow.com/a/70027129) – user692942 Nov 25 '22 at 16:16

1 Answers1

1

When iterating the dictionary collection using a For statement the enumerator used by ASPJSON is the "key", in your case the variable element. If you use;

Response.Write element

the current key will be returned, you can then use;

Response.Write InitialGlaDataJsonForm.data("glaFormContent").item(element)

to retrieve the underlying value associated with the key.


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175