I am trying to automate reporting. I tried to sterilize the JSON as little as possible.
I am getting a JSON response from an API endpoint, and would like to extract the values sections of the responses into a string variable.
This is the JSON response (stored in variable strResult
):
{"totalCount":3,"nextPageKey":null,"resolution":"4h","result":[{"metricId":"builtin:service.keyRequest.count.total:names","dataPointCountRatio":3.6E-6,"dimensionCountRatio":3.0E-5,"data":[{"dimensions":["IMPORTANT_ARL_#1","SERVICE_METHOD-HEX#1"],"dimensionMap":{"dt.entity.service_method":"SERVICE_METHOD-HEX#1","dt.entity.service_method.name":"IMPORTANT_ARL_#1"},"timestamps":[1667289600000,1667304000000,1667318400000,1667332800000,1667347200000,1667361600000],"values":[null,1,30,26,null,null]},{"dimensions":["IMPORTANT_ARL_#2","SERVICE_METHOD-HEX#2"],"dimensionMap":{"dt.entity.service_method":"SERVICE_METHOD-HEX#2","dt.entity.service_method.name":"IMPORTANT_ARL_#2"},"timestamps":[1667289600000,1667304000000,1667318400000,1667332800000,1667347200000,1667361600000],"values":[60,371,1764,1964,1707,1036]},{"dimensions":["IMPORTANT_ARL_#3","SERVICE_METHOD-HEX#3"],"dimensionMap":{"dt.entity.service_method":"SERVICE_METHOD-HEX#3","dt.entity.service_method.name":"IMPORTANT_ARL_#3"},"timestamps":[1667289600000,1667304000000,1667318400000,1667332800000,1667347200000,1667361600000],"values":[9,6,1077,1171,462,null]}]}]}
The RegEx I wrote using regex101.com
(?<=values\"\:\[)(.+?)(?=\])
I realize the double-quote is a problem, so I have a string variable (JSON3) set to this string with Chr(34) replacing the double quote.
JSON3 = "(?<=values\" & Chr(34) & "\:\[)(.+?)(?=\])"
debug.print json3 will show the correct string needed.
I tried solutions I found here, but whenever I pass strResult
as the string, and JSON3
as the pattern, the functions return an empty set.
I also tried VBA-JSON to parse the JSON into an object that I could extract the data from, but I am getting zero values from that. I followed the ReadMe, but I want to pull the JSON from a variable while the ReadMe example pulls it from a file.
In a perfect world, I would like to store all of the keys/values in an object that I can extract the data from. This JSON is just one of many queries I am submitting to the API, so it would be ideal to have something that could be scaled for other uses as well.