rcdmk / aspJSON

A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
MIT License
204 stars 89 forks source link

How to get a specific value? #72

Closed manattweb closed 6 years ago

manattweb commented 6 years ago

I'm getting a JSON object back from Stripe and need to pull out a specific value. How do I do that?

rcdmk commented 6 years ago

Hi! Can you be a bit more specific, please?

Maybe providing some example code.

If you are trying to get the value of an object's property, you can easily use parentheses syntax:

Set jsonObj = new jsonObject
jsonObj.Parse(myaJsonString)
Response.Write jsonObj("my_property")

Hope this helps.

manattweb commented 6 years ago

I figured it out - sorry for the bother.

rcdmk commented 6 years ago

Hi! No worries. It is common to have people asking this. It is my fault actually, for not having this better documented.

Bests.

manattweb commented 6 years ago

One more question: To get a value of a nested group of values, would you treat those as an array? Example: JSON("source(0)") to get the first value in that subset or would you do something else like setting an array variable of the node and then pulling out the value you need?

rcdmk commented 6 years ago

If you have a property that is an array, it will be parsed to a jsonArray object and you can access its items through the Items property:

Response.Write JSON("source").items(0)
' or
Set arr = JSON("source")
Response.Write arr.items(0)
manattweb commented 6 years ago

Is your tool expecting "[" as part of the JSON file? Mine doesn't have those and I'm getting the following error: Object doesn't support this property or method: 'JSON(...).items'

Here's the sanitized partial JSON string I'm working with:

testData = "{""shipping"": null, ""source"": { ""id"": ""123456789"", ""object"": ""card"", ""address_city"": ""little rock"", ""address_country"": null, ""address_line1"": ""123 Main St"", ""address_line1_check"": ""pass"", ""address_line2"": null, ""address_state"": ""ar"", ""address_zip"": ""72223"", ""address_zip_check"": ""pass"", ""brand"": ""Visa"", ""country"": ""GB"", ""customer"": null, ""cvc_check"": ""pass"", ""dynamic_last4"": null, ""exp_month"": 1, ""exp_year"": 2019, ""fingerprint"": ""lux5iu1qzm8BLoaj"", ""funding"": ""credit"", ""last4"": ""4242"", ""metadata"": {}, ""name"": null, ""tokenization_method"": null }, ""source_transfer"": null, ""statement_descriptor"": null, ""status"": ""succeeded"", ""transfer_group"": null }""

And this is the code I'm using to try to get that information inside the "source" node:

set JSON = New JSONobject JSON.Parse(testData) Set addressArr = JSON.Value("source") txn_id = addressArr.items(0)

Thoughts?

rcdmk commented 6 years ago

Oh. Now I see. You have a nested object, not an array. You can access the property value the same way you access the parent one:

Set source = JSON("source")
Response.Write source("id")
' or
Response.Write JSON("source")("id")
manattweb commented 6 years ago

thanks for your patience and assistance. The solution is now complete and working great. I appreciate your hard work on this.

rcdmk commented 6 years ago

You are welcome. Thank you.