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

2
  • 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. Commented Nov 25, 2022 at 16:10
  • 1
    Related - How do you retrieve a JSON property from an array in aspJSON? Commented Nov 25, 2022 at 16:16

1 Answer 1

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

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

Comments

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.