10

My company has a vendor providing a JSON feed of data that I need to load into our MS Access database every two hours. I need to:

  1. load the data from the feed,
  2. parse the JSON into a usable format for Access, and then
  3. insert it into the database.

I came across this question discussing a similar issue, but there's no good description there as to how to implement this in MS Access. Any help gratefully appreciated!

Community
  • 1
  • 1
Martin O'Neill
  • 101
  • 1
  • 1
  • 4
  • You could also browse the JSON modules here: https://github.com/CactusData/VBA.CVRAPI ... using collections to hold the data. It takes a little but VBA and JSON are not made for each other - there are no simple or smart solutions. – Gustav May 28 '15 at 15:28
  • Could you share some sample JSON like you have, and resulting DB table(s) you want to get as result? Consider [this method](http://stackoverflow.com/a/30494373/2165759) also. – omegastripes May 28 '15 at 19:11
  • Your question implies that you are not comfortable with VBA, am I mistaken in that? – Charles Wood Oct 19 '15 at 20:28

2 Answers2

14

Using the VBA JSON library, you certainly can import JSON formatted files into MS Access. The idea is to consider JSON data as a collection of dictionaries and Visual Basic provides the collection and dictionary as data structures.

Below are the steps:

  1. Build a table to match the structure of expected JSON data
  2. On the VBA IDE side of MS Access, import the JsonConverter.bas (from link above) into a new module
  3. Still in the IDE, under Tools / References, check off the VBA Reference: Microsoft Scripting Runtime
  4. Include the following code that reads the JSON text file, parses it as a collection of dictionaries (with keys and valeus), and appends values iteratively into Access table. Place code behind an Access form or module (example uses a one nested level JSON file)

JSON

[
  {
    "col1": somenumber,
    "col2": "somestring",
    "col3": "somestring",
    "col4": "somestring",
    "col5": "somestring"
  }
]

VBA Code

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 "C:\Path\To\JsonFile.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 [col1] Long, [col2] Text(255), [col3] Text(255), " _
                          & "[col4] Text(255), [col5] Text(255); " _
                  & "INSERT INTO TableName (col1, col2, col3, col4, col5) " _
                          & "VALUES([col1], [col2], [col3], [col4], [col5]);"

        Set qdef = db.CreateQueryDef("", strSQL)

        qdef!col1 = element("col1")
        qdef!col2 = element("col2")
        qdef!col3 = element("col3")
        qdef!col4 = element("col4")
        qdef!col5 = element("col5")

        qdef.Execute
    Next element

    Set element = Nothing
    Set p = Nothing
End Function
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • 1
    Stupid question... how do you use this library? – Christine Jan 26 '17 at 17:19
  • 2
    Like the "Readme" says: Import JsonConverter.bas into your project (Open VBA Editor, Alt + F11; File > Import File); Add Dictionary reference/class( include a reference to "Microsoft Scripting Runtime") – Lionel T. Mar 29 '17 at 18:58
-5

Json file handling in MS Access is easy. Just rename the .json extension to .txt and use the text import function with the delimiter set to (:) and the text delimiter to ("). One line of code... Happy coding!

Heinz
  • 1
  • 2
    Welcome to Stack Overflow! While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Mar 27 '17 at 10:11
  • 3
    This answer assumes the data is a flat file and therefore no parsing is required. The point of JSON is structured data. So this answer is unhelpful. – Smandoli May 18 '17 at 00:28