-
Notifications
You must be signed in to change notification settings - Fork 590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access Data via Single String Command #241
Comments
I threw together a quick function that did what I need. But if there is an official function for this, I would be interested!
|
In other words, you want an accessor with a "path" argument. |
That might be exactly what I am looking for! Is there an example of using XPath (or something similar) with this project that I could reference? I originally was using another project that supported it natively in their project, but it had other compatibility issues: https://medium.com/swlh/excel-vba-parse-json-easily-c2213f4d8e7a |
@sombies thanks for the pointer to that article. A nice simple implementation of a JSON parser in VBA. I would also like to point out for those people using Excel 2016 or later you can use Excel's built-in JSON parser, which is found in Power Query, and write a Power Query to produce a table in Excel. Using the example JSON from the article @sombies mentioned: {
"data" : {
"receipt_time" : "2018-09-28T10:00:00.000Z",
"site" : "Los Angeles",
"measures" : [ {
"test_id" : "C23_PV",
"metrics" : [ {
"val1" : [ 0.76, 0.75, 0.71 ],
"temp" : [ 0, 2, 5 ],
"TS" : [ 1538128801336, 1538128810408, 1538128818420 ]
} ]
},
{
"test_id" : "HBI2_XX",
"metrics" : [ {
"val1" : [ 0.65, 0.71 ],
"temp" : [ 1, -7],
"TS" : [ 1538128828433, 1538128834541 ]
} ]
}]
}
} The following Power Query will transform that JSON into an Excel table:
The Excel table will look like:
|
@sombies, a comment on the function you wrote. You could use a VBA ParamArray instead of a string to specify the path elements to the value you want. Additionally, I would suggest returning a MsgBox JsonValue(jsonResult, "features", 1, "attributes", "ASSET_ID")
Public Function JsonValue(json As Object, ParamArray path() As Variant) As Variant
End Function Your implementation can also key off the fact that when the value of the One last thing about the first parameter of your function which is declared as an Object. Valid Json, according to the specification, is:
Declaring a VBA Json BNF json
element
element
ws value ws
value
object
array
string
number
"true"
"false"
"null" |
Is there a function built in to access data dynamically via a single string? For example, I am accessing the data below:
MsgBox jsonResult("features")(1)("attributes")("ASSET_ID")
But I want to be able to request the same data with a dynamic string, like:
MsgBox JsonString(jsonResult, "('features')(1)('attributes')('ASSET_ID')")
Does a function like this already exist?
The text was updated successfully, but these errors were encountered: