
VBAで
・JSONをパース
できます!

GitHubで公開されている
・VBA-JSON
を使用してパースします!
VBA-JSON(JsonConverter.bas)の取得
以下のURLにアクセスする。
「Code」-「Download ZIP」をクリックして「VBA-JSON-master.zip」をダウンロードする。

「VBA-JSON-master.zip」を展開して、展開後のフォルダの中にある「JsonConverter.bas」をVBEにドロップ & ドラッグする。


VBA-JSON(JsonConverter.bas)の微修正
「JsonConverter.bas」の以下の2行を修正します。
※「コメントアウト」と「追記」をします。
'Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Dictionary ←コメントアウト
Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Scripting.Dictionary ←追記
'Set json_ParseObject = New Dictionary ←コメントアウト
Set json_ParseObject = New Scripting.Dictionary ←追記
参照設定に「Selenium Type Library」を追加している場合、本手順を実施しないと以下のエラーが発生します。
実行時エラー ‘0’:
KyeNotFoundError
Dictionary key not found:title

参照設定の追加
参照設定に「Microsoft Scripting Runtime」を追加します。
※「VBA-JSON(JsonConverter.bas)」内で「Microsoft Scripting Runtime」内のオブジェクトを使用しているため。
VBEの「ツール」-「参照設定」をクリックして、「Microsoft Scripting Runtime」にチェックを入れ「OK」をクリックします。

VBAコード
ここでは例として
・EDINETのWeb APIを使用してJSONデータを取得して
・JSONデータをパースして得られた結果の一部をイミディエイトウィンドウへ出力
します。
Option Explicit
Sub sample()
Dim url As String
Dim http As Object
Dim json As Variant
'メタデータ(=JSON)を取得するEDINETのWeb API
url = "https://disclosure.edinet-fsa.go.jp/api/v1/documents.json?date=2023-04-11&type=1"
'HTTPリクエスト
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
http.Open "GET", url, False
http.Send
'HTTPレスポンス(=JSON)をパース
Set json = JsonConverter.ParseJson(http.responseText)
'パースした結果をイミディエイトウィンドウへ出力
'┗[metadata]-[title]
'┗[metadata]-[parameter]-[date]
'┗[metadata]-[status]
Debug.Print json("metadata")("title")
Debug.Print json("metadata")("parameter")("date")
Debug.Print json("metadata")("status")
'後片付け
Set http = Nothing
End Sub
実行結果
JSONをパースできました。
※JSONデータをパースして得られた結果の一部をイミディエイトウィンドウへ出力できました。

参考①
上記で使用した以下の詳細は、公式サイトをご確認ください。
●「winhttprequestPオブジェクト」の「Open」メソッド
●「winhttprequestPオブジェクト」の「Send」メソッド
参考②
EDINETとは「金融商品取引法に基づく有価証券報告書等の開示書類に関する電子開示システム」のことです。
詳細は金融庁のWebサイトをご確認ください。