How to reference and parse Json VBA Ms Access - json

I want to be helped to get the Arrays of Json into my table in Ms Access, how do I reference the arrays.
I have been having problems with arrays for sometime now that is why I need this sorted once and for all
With rs
.AddNew
![id] = item("id")
![riority] = item("urgent")
![Status] = item("Closed")
.Update
End With
{
"Tickets": [
{
"id": 866883,
"type": "problem",
"priority": "urgent",
"status": "closed",
"follower_ids": [
7967649388,
7967864608,
365003995267,
7897694567,
7897776387,
7899032227,
7969040908,
7898078107
]
}
]
}
I want someone to show me how to reference values from arrays in Ms access VBA

To reference arrays in VBA JSON (using dictionary), you need to specify which element in the array you want:
Using your example:
item(1)("id") will equal 866883 and item(1)("status") will equal closed. To reference the next item, use item(2)("id")

Related

Checking if a key exists in a nested JSON element using Newtonsoft JSON and VB.NET

Using Newtonsoft Json with VB.NET I am trying to read some nested keys/elements within a block of JSON.
The JSON looks like this and is held in string strSuppliedJSON:
{
"seller": {
"id": 123,
"name": "Seller Name",
"address1": "Seller address1",
"country": "Seller country"
},
"buyer": {
"id": 987,
"name": "Buyer name",
"address1": "Buyer address1",
"country": "Buyer country"
},
"interview": {
"call_id": 123,
"vin": "The vin from the machine section",
"call_date": "2019-12-31 23:59:59",
"questions": ["Question1", "Question2", "Question3", "Question5", "Question5"],
"triggers": [{
"question": "Question1",
"answers": ["Answer1", "Answer2"]
}]
}
}
Before reading the values I need to make sure some of the keys exist using ContainsKey.
The following works fine:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
' Create a dictionary
Dim dictionary As IDictionary(Of String, JToken) = JObject.Parse(strSuppliedJSON)
' Check if key exists - interview:triggers
If JObject.Parse(dictionary("interview").ToString()).ContainsKey("triggers") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers"
End If
However, when trying to check or read the interview:triggers:question things are falling over.
Using this, fails:
' Check if key exists - interview:triggers
If JObject.Parse(dictionary("interview").ToString()).ContainsKey("triggers") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers"
Else
' interview:triggers DOES exist, now check if the question exists - interview:triggers:question
If JObject.Parse(dictionary("interview")("triggers").ToString()).ContainsKey("question") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If
End If
The line that throws the error is:
If JObject.Parse(dictionary("interview")("triggers").ToString()).ContainsKey("question") = False Then
And the error is:
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1
So I then tried to create a sub-dictionary of just the interview element.
I used this:
' Create a sub-dictionary of just the Interview element
Dim subDictionary As IDictionary(Of String, JToken) = JObject.Parse(dictionary("interview").ToString())
And if I then do Response.Write(subDictionary) I now see a smaller subset of my JSON, as expected:
{
"call_id": 123,
"vin": "The vin from the machine section",
"call_date": "2019-12-31 23:59:59",
"questions": ["Question1", "Question2", "Question3", "Question5", "Question5"],
"triggers": [{
"question": "Question1",
"answers": ["Answer1", "Answer2"]
}]
}
But then when trying to use my new subDictionary in the exact same way to see if the question key exists:
' Check if key exists - interview:triggers:question
If JObject.Parse(subDictionary("triggers").ToString()).ContainsKey("question") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If
I get the exact same error of:
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1
Even though the line of code is identical!
How do I check if the nested key interview:triggers:question exists in my JSON, and what its value is?
Look closely at "triggers" in the JSON: it's actually an array of objects, not an object. You need to index the array before you can access "question".
Also note that every time you call ToString, you're reserializing something that you just deserialized with Parse. You don't need to do that. Parse the JSON once into a JObject and then reuse that object.
My VB-fu isn't great; I wrote this in C# and then converted it but the critical part is Dim first As JToken = triggers(0). This gets the first array element, on which you can get the value associated with "question".
Dim suppliedObject As JObject = JObject.Parse(strSuppliedJSON)
Dim interview As JToken = suppliedObject("interview")
Dim triggers As JToken = If(interview IsNot Nothing, interview("triggers"), Nothing)
If triggers Is Nothing Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers"
Else
Dim first As JToken = triggers(0)
Dim question As JToken = If(first IsNot Nothing, first("question"), Nothing)
If question Is Nothing Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If
End If
It fails because your path is not proper.
[] is an Array. You write out the name for single items (within {}), for arrays you write index (number).
And why are you parsing multiple times? Use what you already have, for example:
Dim JsonResp As JObject = JObject.Parse(<JSON>)
'Now there's multiple ways to do the same, here's one
If JsonResp ("interview")("triggers")(0)("question") Is Nothing Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If

how to extract elements in json string in vba

I have the following json string:
[
{
"Features": {
"Level": [
{
"endDate": "2018-12-11",
"minimum": "0.000000000000",
"maximum": "0.000000000000",
"value": "228.108000000000",
"payDate": "0"
},
{
"endDate": "2018-12-11",
"minimum": "0.000000000000",
"maximum": "0.000000000000",
"value": "3143.513000000000",
"payDate": "0"
}
]
}
},
]
I was trying to extract the two values in excel using vba and have the following code (where result2 would be the json string)
Public Sub GetS (result2 As String, m As Integer)
Dim activeWS As Worksheet
Set activeWS = ThisWorkbook.Worksheets("Data")
Dim jsonStr As String, json As Object, headers()
jsonStr = result2
Set json = JsonConverter.ParseJson(jsonStr)(1)
activeWS.Cells(m, 19) = json("Features")("Level")(0)("value")
activeWS.Cells(m, 20) = json("Features")("Level")(1)("value")
End Sub
the second part of the vba works, where it grab the value of 3143.51 (the second number), i'm wondering how can I get the first value (228.10).
I've tried using ("Initial Level")(0)("value") but it doesn't work.
Thank you so much.
JSON arrays get converted to VBA Collections - these are indexed from 1 not from zero
Try this:
jsonStr = Range("I10").Value 'your json sample
Set json = JsonConverter.ParseJson(jsonStr)(1)
Debug.Print json("Features")("Level")(1)("value") '>>228.108000000000
Debug.Print json("Features")("Level")(2)("value") '>>3143.513000000000
(tested and confirmed)
Here is how to access data in the JSON string:
Json("match")("awayTeam")("coach")("countryOfBirth")
Json("match")("awayTeam")("coach")("nationality")
Json("match")("awayTeam")("captain")("id")
Json("match")("awayTeam")("captain")("name")
Json("match")("awayTeam")("captain")("shirtNumber")
Json("match")("awayTeam")("lineup")(1)("id")
Json("match")("awayTeam")("lineup")(1)("name")
Json("match")("awayTeam")("lineup")(1)("position")
Json("match")("awayTeam")("lineup")(1)("shirtNumber")
Json("match")("awayTeam")("lineup")(2)("id")
Json("match")("awayTeam")("lineup")(2)("name")
Json("match")("awayTeam")("lineup")(2)("position")
Json("match")("awayTeam")("lineup")(2)("shirtNumber")
Json("match")("awayTeam")("lineup")(3)("id")
Json("match")("awayTeam")("lineup")(3)("name")
Json("match")("awayTeam")("lineup")(3)("position")
Json("match")("awayTeam")("lineup")(3)("shirtNumber")
Json("match")("awayTeam")("lineup")(4)("id")
Json("match")("awayTeam")("lineup")(4)("name")
Json("match")("awayTeam")("lineup")(4)("position")
Json("match")("awayTeam")("lineup")(4)("shirtNumber")
Json("match")("awayTeam")("lineup")(5)("id")
Json("match")("awayTeam")("lineup")(5)("name")
Json("match")("awayTeam")("lineup")(5)("position")
Json("match")("awayTeam")("lineup")(5)("shirtNumber")
Json("match")("awayTeam")("lineup")(6)("id")
Json("match")("awayTeam")("lineup")(6)("name")
Json("match")("awayTeam")("lineup")(6)("position")
Json("match")("awayTeam")("lineup")(6)("shirtNumber")
Json("match")("awayTeam")("lineup")(7)("id")
Json("match")("awayTeam")("lineup")(7)("name")
Json("match")("awayTeam")("lineup")(7)("position")
Json("match")("awayTeam")("lineup")(7)("shirtNumber")
Json("match")("awayTeam")("lineup")(8)("id")
Json("match")("awayTeam")("lineup")(8)("name")
Json("match")("awayTeam")("lineup")(8)("position")
Json("match")("awayTeam")("lineup")(8)("shirtNumber")
Json("match")("awayTeam")("lineup")(9)("id")
Json("match")("awayTeam")("lineup")(9)("name")
Json("match")("awayTeam")("lineup")(9)("position")
Json("match")("awayTeam")("lineup")(9)("shirtNumber")
Json("match")("awayTeam")("lineup")(10)("id")
Json("match")("awayTeam")("lineup")(10)("name")
Json("match")("awayTeam")("lineup")(10)("position")
Json("match")("awayTeam")("lineup")(10)("shirtNumber")
Json("match")("awayTeam")("lineup")(11)("id")
Json("match")("awayTeam")("lineup")(11)("name")
Json("match")("awayTeam")("lineup")(11)("position")
Json("match")("awayTeam")("lineup")(11)("shirtNumber")
Json("match")("awayTeam")("bench")(1)("id")
Json("match")("awayTeam")("bench")(1)("name")
Json("match")("awayTeam")("bench")(1)("position")
Json("match")("awayTeam")("bench")(1)("shirtNumber")
Json("match")("awayTeam")("bench")(2)("id")
Json("match")("awayTeam")("bench")(2)("name")
Json("match")("awayTeam")("bench")(2)("position")
Json("match")("awayTeam")("bench")(2)("shirtNumber")
Json("match")("awayTeam")("bench")(3)("id")
Json("match")("awayTeam")("bench")(3)("name")
Json("match")("awayTeam")("bench")(3)("position")
Json("match")("awayTeam")("bench")(3)("shirtNumber")
Json("match")("awayTeam")("bench")(4)("id")
Json("match")("awayTeam")("bench")(4)("name")
Json("match")("awayTeam")("bench")(4)("position")
Json("match")("awayTeam")("bench")(4)("shirtNumber")
Json("match")("awayTeam")("bench")(5)("id")
Json("match")("awayTeam")("bench")(5)("name")
Json("match")("awayTeam")("bench")(5)("position")
Json("match")("awayTeam")("bench")(5)("shirtNumber")
Json("match")("awayTeam")("bench")(6)("id")
Json("match")("awayTeam")("bench")(6)("name")
Json("match")("awayTeam")("bench")(6)("position")
Json("match")("awayTeam")("bench")(6)("shirtNumber")
Json("match")("awayTeam")("bench")(7)("id")
Json("match")("awayTeam")("bench")(7)("name")
Json("match")("awayTeam")("bench")(7)("position")
Json("match")("awayTeam")("bench")(7)("shirtNumber")
Json("match")("goals")(1)("minute")
Json("match")("goals")(1)("extraTime")
Json("match")("goals")(1)("type")
Json("match")("goals")(1)("team")("id")
Json("match")("goals")(1)("team")("name")
Json("match")("goals")(1)("scorer")("id")
Json("match")("goals")(1)("scorer")("name")
Json("match")("goals")(1)("assist")("id")
Json("match")("goals")(1)("assist")("name")
Json("match")("goals")(2)("minute")
Json("match")("goals")(2)("extraTime")
Json("match")("goals")(2)("type")
Json("match")("goals")(2)("team")("id")
Json("match")("goals")(2)("team")("name")
Json("match")("goals")(2)("scorer")("id")
Json("match")("goals")(2)("scorer")("name")
Json("match")("goals")(2)("assist")("id")
Json("match")("goals")(2)("assist")("name")
Json("match")("goals")(3)("minute")
Json("match")("goals")(3)("extraTime")
Json("match")("goals")(3)("type")
Json("match")("goals")(3)("team")("id")
Json("match")("goals")(3)("team")("name")
Json("match")("goals")(3)("scorer")("id")
Json("match")("goals")(3)("scorer")("name")
Json("match")("goals")(3)("assist")
Json("match")("bookings")(1)("minute")
Json("match")("bookings")(1)("team")("id")
Json("match")("bookings")(1)("team")("name")
Json("match")("bookings")(1)("player")("id")
Json("match")("bookings")(1)("player")("name")
Json("match")("bookings")(1)("card")
Json("match")("bookings")(2)("minute")
Json("match")("bookings")(2)("team")("id")
Json("match")("bookings")(2)("team")("name")
Json("match")("bookings")(2)("player")("id")
Json("match")("bookings")(2)("player")("name")
Json("match")("bookings")(2)("card")
Json("match")("bookings")(3)("minute")
Json("match")("bookings")(3)("team")("id")
Json("match")("bookings")(3)("team")("name")
Json("match")("bookings")(3)("player")("id")
Json("match")("bookings")(3)("player")("name")
Json("match")("bookings")(3)("card")
Json("match")("bookings")(4)("minute")
Json("match")("bookings")(4)("team")("id")
Json("match")("bookings")(4)("team")("name")
Json("match")("bookings")(4)("player")("id")
Json("match")("bookings")(4)("player")("name")
Json("match")("bookings")(4)("card")
Json("match")("bookings")(5)("minute")
Json("match")("bookings")(5)("team")("id")
Json("match")("bookings")(5)("team")("name")
Json("match")("bookings")(5)("player")("id")
Json("match")("bookings")(5)("player")("name")
Json("match")("bookings")(5)("card")
Json("match")("bookings")(6)("minute")
Json("match")("bookings")(6)("team")("id")
Json("match")("bookings")(6)("team")("name")
Json("match")("bookings")(6)("player")("id")
Json("match")("bookings")(6)("player")("name")
Json("match")("bookings")(6)("card")
Json("match")("bookings")(7)("minute")
Json("match")("bookings")(7)("team")("id")
Json("match")("bookings")(7)("team")("name")
Json("match")("bookings")(7)("player")("id")
Json("match")("bookings")(7)("player")("name")
Json("match")("bookings")(7)("card")
Json("match")("substitutions")(1)("minute")
Json("match")("substitutions")(1)("team")("id")
Json("match")("substitutions")(1)("team")("name")
Json("match")("substitutions")(1)("playerOut")("id")
Json("match")("substitutions")(1)("playerOut")("name")
Json("match")("substitutions")(1)("playerIn")("id")
Json("match")("substitutions")(1)("playerIn")("name")
Json("match")("substitutions")(2)("minute")
Json("match")("substitutions")(2)("team")("id")
Json("match")("substitutions")(2)("team")("name")
Json("match")("substitutions")(2)("playerOut")("id")
Json("match")("substitutions")(2)("playerOut")("name")
Json("match")("substitutions")(2)("playerIn")("id")
Json("match")("substitutions")(2)("playerIn")("name")
Json("match")("substitutions")(3)("minute")
Json("match")("substitutions")(3)("team")("id")
Json("match")("substitutions")(3)("team")("name")
Json("match")("substitutions")(3)("playerOut")("id")
Json("match")("substitutions")(3)("playerOut")("name")
Json("match")("substitutions")(3)("playerIn")("id")
Json("match")("substitutions")(3)("playerIn")("name")
Json("match")("substitutions")(4)("minute")
Json("match")("substitutions")(4)("team")("id")
Json("match")("substitutions")(4)("team")("name")
Json("match")("substitutions")(4)("playerOut")("id")
Json("match")("substitutions")(4)("playerOut")("name")
Json("match")("substitutions")(4)("playerIn")("id")
Json("match")("substitutions")(4)("playerIn")("name")
Json("match")("substitutions")(5)("minute")
Json("match")("substitutions")(5)("team")("id")
Json("match")("substitutions")(5)("team")("name")
Json("match")("substitutions")(5)("playerOut")("id")
Json("match")("substitutions")(5)("playerOut")("name")
Json("match")("substitutions")(5)("playerIn")("id")
Json("match")("substitutions")(5)("playerIn")("name")
Json("match")("substitutions")(6)("minute")
Json("match")("substitutions")(6)("team")("id")
Json("match")("substitutions")(6)("team")("name")
Json("match")("substitutions")(6)("playerOut")("id")
Json("match")("substitutions")(6)("playerOut")("name")
Json("match")("substitutions")(6)("playerIn")("id")
Json("match")("substitutions")(6)("playerIn")("name")
Json("match")("referees")(1)("id")
Json("match")("referees")(1)("name")
Json("match")("referees")(1)("nationality")
Json("match")("referees")(2)("id")
Json("match")("referees")(2)("name")
Json("match")("referees")(2)("nationality")
Json("match")("referees")(3)("id")
Json("match")("referees")(3)("name")
Json("match")("referees")(3)("nationality")
Json("match")("referees")(4)("id")
Json("match")("referees")(4)("name")
Json("match")("referees")(4)("nationality")
The two answers below show how to use a function that I wrote, PrintJSONAccessors(), to unravel the collections and arrays produced by JsonConverter.ParseJson().
Using VBA and VBA-JSON to access JSON data from Wordpress API
How to get, JSON values to Work in VBA-JSON

Parsing, Extracting & Returning JSON as Hash

I am trying to make a localized version of this app: SMS Broadcast Ruby App
I have been able to get the JSON data from a local file & sanitize the number as well as open the JSON data. However I have been unable to extract the values and pair them as a scrubbed hash. Here's what I have so far.
def data_from_spreadsheet
file = open(spreadsheet_url).read
JSON.parse(file)
end
def contacts_from_spreadsheet
contacts = {}
data_from_spreadsheet.each do |entry|
puts entry['name']['number']
contacts[sanitize(number)] = name
end
contacts
end
Here's the JSON data sample I'm working with.
[
{
"name": "Michael",
"number": 9045555555
},
{
"name": "Natalie",
"number": 7865555555
}
]
Here's how I would like the JSON to be expressed after the contacts_from_spreadsheet method.
{
'19045555555' => 'Michael',
'19045555555' => 'Natalie'
}
Any help would be much appreciated.
You could create array of pairs (hashes) using map and then call reduce to get a single hash.
data = [{
"name": "Michael",
"number": 9045555555
},
{
"name": "Natalie",
"number": 7865555555
}]
data.map{|e| {e[:number] => e[:name]}}.reduce Hash.new, :merge
Result: {9045555555=>"Michael", 7865555555=>"Natalie"}
You don't seem to have number or name extracted in any way. I think first you'll need to update your code to get those details.
i.e. If entry is a JSON object (or rather was before parsing), you can do the following:
def contacts_from_spreadsheet
contacts = {}
data_from_spreadsheet.each do |entry|
contacts[sanitize(entry['number'])] = entry['name']
end
contacts
end
Not really keeping this function within JSON, but I have solved the problem. Here's what I used.
def data_from_spreadsheet
file = open(spreadsheet_url).read
YAML.load(file)
end
def contacts_from_spreadsheet
contacts = {}
data_from_spreadsheet.each do |entry|
name = entry['name']
number = entry['phone_number'].to_s
contacts[sanitize(number)] = name
end
contacts
end
This returned back clean array here:
{"+19045555555"=>"Michael", "+17865555555"=>"Natalie"}
Thanks everyone who added input!

Check if JSON string in Orbeon repeating grid contains a specific value

Working with the repeating grids through the form builder.
I have a custom control that has a string value represented in json.
{
"data": {
"type": "File",
"itemID": "12345",
"name": "Annual Summary",
"parentFolderID": "fileID",
"owner": "Owner",
"lastModifiedDate": "2016-10-17 22:48:05Z"
}
}
In the controls outside of the repeating grid, i need to check if name = "Annual Summary"
Previously, i had a drop down control and using Calculated Value $dropdownControl = "Annual Summary" it was able to return true if any of the repeated rows contained the value. My understanding is that using the = operator, it will validate against all rows.
Now with the json output of the control, I am attempting to use
contains($jsonStringValue, 'Annual Summary')
However, this only works with one entry and will be null if there are multiple rows.
2 questions:
How would validate whether "Annual Summary" (or any other text) is present within any of the repeated rows?
Is there any way to navigate the json or parse it to XML and navigate it?
Constraint:
within the Calculated Value or Visibility fields within form builder
manipulating the source that is generated by the form builder
You probably want to parse the JSON string first. See also this other Stackoverflow question.
Until Orbeon Forms 2016.3 is released, you would write:
(
for $v in $jsonStringValue
return converter:jsonStringToXml($v)
)//name = 'Annual Summary'
With the above, you also need to scope the namespace:
xmlns:converter="org.orbeon.oxf.json.Converter"
Once Orbeon Forms 2016.3 is released you can switch to:
$jsonStringValue/xxf:json-to-xml()//name = 'Annual Summary'

Dynamic json parse in vb.net

I need to parse a simple Json, but the answer is not allways the same, in the past i have created a class with tools like http://jsonutils.com/ but in this case one of the parameter changes.. this is an example of the JSON:
{"success": 1,
"message": "Registros recuperados",
"data": {
"rows": [
{
"id_jugador": "454",
"nombre": "ALEXANDER",
"apellido": "CABRERA",
"id_equipo": "5",
"equipo": "TIBURONES",
"hr": "21"
}
]
},
"total": 1
}
The problem is that the last item (hr in this case, changes some time will be hits, or have some other name), until now i have been using something like this..
jsonObjectIntance = JsonConvert.DeserializeObject(Of jsonObjectClass)(jsonString)
how do I parse it if the last parameter changes?? the hr itself con be other baseball statistics like hits, or doubles, triples, strikeouts, etc... , thanks!
thanks to #OneFineDay for the link, the difficult part for me was the access to data inside the object.
Dim item As Object = JsonConvert.DeserializeObject(Of Object)(json)
Dim success as string = item("success")
Dim data As Object = item("data")
Dim rows As JArray = data("rows")
DataGridView1.DataSource = rows