I am trying to import a JSON file into an MS Access table. I have looked online and found this Stack overflow link that speaks to this. Parsing JSON feed automatically into MS Access I have copied and pasted the code from this string and modified it to pull my JSON file and the code does appear to parse the file. However, I'm having problems getting all elements of the parsed file into the Access table. It seems to only pull in elements that are not part of an object or array. In other words, the NPI element is not wrapped in brackets or curly brackets so it imports successfully. Please see code and JSON data structure below.
Private Function JSONImport()
Dim db As Database, qdef As QueryDef
Dim FileNum As Integer
Dim DataLine As String, jsonStr As String, strSQL As String
Dim P As Object, element As Variant
Set db = CurrentDb
' READ FROM EXTERNAL FILE
FileNum = FreeFile()
'Open "P:\PROF REIMB\PROF REIMB\HIX\CY 2021 Analysis\Centene\JSON\provider_facility - jun 52020.json"
For Input As #FileNum
' PARSE FILE STRING
jsonStr = ""
While Not EOF(FileNum)
Line Input #FileNum, DataLine
jsonStr = jsonStr & DataLine & vbNewLine
Wend
Close #FileNum
Set P = ParseJson(jsonStr)
' ITERATE THROUGH DATA ROWS, APPENDING TO TABLE
For Each element In P
strSQL = "PARAMETERS (first), [middle] Text(255), [last] Text(255), [suffix] Text(255), [npi]
Text(255), [type] Text(255), [addresses] Text(255), [addresses_2] Text(255), [city] Text(255),
[state] Text(255), [zip] Text(255), [phone] Text(255), [specialty] Text(255), [accepting]
Text(255), [plans] Text(255), [plan_id_type] Text(255), [plan_id] Text(255), [network_tier]
Text(255), [years] Text(255); " _
& "INSERT INTO FrmJSONFile (first, middle, last, suffix, npi, type, addresses,
addresses_2, city, state, zip, phone, specialty, accepting, plans, plan_id_type,
plan_id, network_tier, years) " _
& "VALUES([first], [middle], [last], [suffix], [npi], [type], [addresses], [addresses_2], [city],
[state], [zip], [phone], [specialty], [accepting], [plans], [plan_id_type], [plan_id],
[network_tier], [years]);"
Set qdef = db.CreateQueryDef("", strSQL)
qdef!first = element("first")
qdef!middle = element("middle")
qdef!last = element("last")
qdef!suffix = element("suffix")
qdef!npi = element("npi")
qdef!Type = element("type")
qdef!addresses = element("addresses")
qdef!addresses_2 = element("addresses_2")
qdef!city = element("city")
qdef!State = element("state")
qdef!Zip = element("zip")
qdef!phone = element("phone")
qdef!specialty = element("specialty")
qdef!accepting = element("accepting")
qdef!plans = element("plans")
qdef!plan_id_type = element("plan_id_type")
qdef!plan_id = element("plan_id")
qdef!network_tier = element("network_tier")
qdef!years = element("years")
qdef.Execute
Next element
Set element = Nothing
Set P = Nothing
End Function
JSON file:
[{
"name":{
"first":"John","middle":"G","last":"Doe","suffix":"MD"
},
"npi":"1234567891",
"type":"INDIVIDUAL",
"addresses":[
{"address":"123 Main St",
"address_2":"",
"city":"CHARLESTON",
"state":"SC",
"zip":"29406",
"phone":"8037779311"}
],
"specialty":["ANESTHESIOLOGY"],
"accepting":"not accepting",
"plans":[
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678912",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678913",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678914",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678915",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678916",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678917",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678918",
"network_tier":"PREFERRED","years":[2020]},
{"plan_id_type":"HIOS-PLAN-ID","plan_id":"12345678919",
"network_tier":"PREFERRED","years":[2020]}
],
"languages":["ENGLISH"],
"gender":"Male",
"last_updated_on":"2020-05-26"
}]