{"id":950,"date":"2020-12-02T17:54:31","date_gmt":"2020-12-02T17:54:31","guid":{"rendered":"http:\/\/www.vbastring.com\/blog\/?p=950"},"modified":"2020-12-02T17:54:31","modified_gmt":"2020-12-02T17:54:31","slug":"how-to-parse-json-with-excel-vba","status":"publish","type":"post","link":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/","title":{"rendered":"How To Parse JSON With Excel VBA"},"content":{"rendered":"<p>I didn&#8217;t come up with this code, but it is pretty cool.  <\/p>\n<p>It shows how to parse a JSON string with VBA.  <\/p>\n<p>The impetus for this was to access the price data for various coins on coinmarketcap.com<\/p>\n<p>&#8230;and to get the price data for 50+ coins required getting a coinmarketcap api, and resulted in a JSON string.<\/p>\n<p>I just included the essential stuff below, maybe it will help you out too.<\/p>\n<p>Go here for more information: <a href=\"https:\/\/medium.com\/swlh\/excel-vba-parse-json-easily-c2213f4d8e7a\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/medium.com\/swlh\/excel-vba-parse-json-easily-c2213f4d8e7a<\/a><\/p>\n<pre class=\"lang:vb decode:true \" >'-------------------------------------------------------------------\r\n' VBA JSON Parser\r\n'-------------------------------------------------------------------\r\nOption Explicit\r\nPrivate p&amp;, token, dic\r\nFunction ParseJSON(json$, Optional key$ = \"obj\") As Object\r\n    p = 1\r\n    token = Tokenize(json)\r\n    Set dic = CreateObject(\"Scripting.Dictionary\")\r\n    If token(p) = \"{\" Then ParseObj key Else ParseArr key\r\n    Set ParseJSON = dic\r\nEnd Function\r\nFunction ParseObj(key$)\r\n    Do: p = p + 1\r\n        Select Case token(p)\r\n            Case \"]\"\r\n            Case \"[\":  ParseArr key\r\n            Case \"{\":  ParseObj key\r\n            Case \"{\"\r\n                       If token(p + 1) = \"}\" Then\r\n                           p = p + 1\r\n                           dic.Add key, \"null\"\r\n                       Else\r\n                           ParseObj key\r\n                       End If\r\n                \r\n            Case \"}\":  key = ReducePath(key): Exit Do\r\n            Case \":\":  key = key &amp; \".\" &amp; token(p - 1)\r\n            Case \",\":  key = ReducePath(key)\r\n            Case Else: If token(p + 1) &lt;&gt; \":\" Then dic.Add key, token(p)\r\n        End Select\r\n    Loop\r\nEnd Function\r\nFunction ParseArr(key$)\r\n    Dim e&amp;\r\n    Do: p = p + 1\r\n        Select Case token(p)\r\n            Case \"}\"\r\n            Case \"{\":  ParseObj key &amp; ArrayID(e)\r\n            Case \"[\":  ParseArr key\r\n            Case \"]\":  Exit Do\r\n            Case \":\":  key = key &amp; ArrayID(e)\r\n            Case \",\":  e = e + 1\r\n            Case Else: dic.Add key &amp; ArrayID(e), token(p)\r\n        End Select\r\n    Loop\r\nEnd Function\r\n'-------------------------------------------------------------------\r\n' Support Functions\r\n'-------------------------------------------------------------------\r\nFunction Tokenize(s$)\r\n    Const Pattern = \"\"\"(([^\"\"\\\\]|\\\\.)*)\"\"|[+\\-]?(?:0|[1-9]\\d*)(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?|\\w+|[^\\s\"\"']+?\"\r\n    Tokenize = RExtract(s, Pattern, True)\r\nEnd Function\r\nFunction RExtract(s$, Pattern, Optional bGroup1Bias As Boolean, Optional bGlobal As Boolean = True)\r\n  Dim c&amp;, m, n, v\r\n  With CreateObject(\"vbscript.regexp\")\r\n    .Global = bGlobal\r\n    .MultiLine = False\r\n    .IgnoreCase = True\r\n    .Pattern = Pattern\r\n    If .test(s) Then\r\n      Set m = .Execute(s)\r\n      ReDim v(1 To m.Count)\r\n      For Each n In m\r\n        c = c + 1\r\n        v(c) = n.Value\r\n        If bGroup1Bias Then If Len(n.submatches(0)) Or n.Value = \"\"\"\"\"\" Then v(c) = n.submatches(0)\r\n      Next\r\n    End If\r\n  End With\r\n  RExtract = v\r\nEnd Function\r\nFunction ArrayID$(e)\r\n    ArrayID = \"(\" &amp; e &amp; \")\"\r\nEnd Function\r\nFunction ReducePath$(key$)\r\n    If InStr(key, \".\") Then ReducePath = Left(key, InStrRev(key, \".\") - 1) Else ReducePath = key\r\nEnd Function\r\nFunction ListPaths(dic)\r\n    Dim s$, v\r\n    Dim intRow As Integer\r\n    \r\n    For Each v In dic\r\n        s = s &amp; v &amp; \" --&gt; \" &amp; dic(v) &amp; vbLf\r\n    Next\r\n    Debug.Print s\r\n    \r\nEnd Function\r\n\r\n'This is my contribution!\r\nSub StartHere()\r\n    Dim strURL As String\r\n    \r\n    strURL = ThisWorkbook.Path &amp; \"\\currencies.txt\"\r\n\r\n    Dim strFileContent As String\r\n    Dim intFile As Integer\r\n    \r\n    intFile = FreeFile\r\n    Open strURL For Input As #intFile\r\n    strFileContent = Input(LOF(intFile), intFile)\r\n    Close #intFile\r\n\r\n    Set dic = ParseJSON(strFileContent)\r\n    Debug.Print ListPaths(dic)\r\nEnd Sub\r\n\r\n\r\n<\/pre>\n<p>Go to this post to see the post for the Load Text File:<br \/>\n<a href=\"http:\/\/www.vbastring.com\/blog\/how-to-load-a-text-file-in-excel-vba-with-load-data-infile\/\" rel=\"noopener noreferrer\" target=\"_blank\">http:\/\/www.vbastring.com\/blog\/how-to-load-a-text-file-in-excel-vba-with-load-data-infile\/<\/a><\/p>\n<p><strong>Here is a bit of the input file and the same bit when run through the parser:<\/strong><\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png?resize=1249%2C181\" alt=\"\" width=\"1249\" height=\"181\" class=\"aligncenter size-full wp-image-954\" \/><\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-OUTPUT.png?resize=600%2C439\" alt=\"\" width=\"600\" height=\"439\" class=\"aligncenter size-full wp-image-955\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t come up with this code, but it is pretty cool. It shows how to parse a JSON string with VBA. The impetus for this was to access the price data for various coins on coinmarketcap.com &#8230;and to get the price data for 50+ coins required getting a coinmarketcap api, and resulted in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-950","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How To Parse JSON With Excel VBA - My Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Parse JSON With Excel VBA - My Blog\" \/>\n<meta property=\"og:description\" content=\"I didn&#8217;t come up with this code, but it is pretty cool. It shows how to parse a JSON string with VBA. The impetus for this was to access the price data for various coins on coinmarketcap.com &#8230;and to get the price data for 50+ coins required getting a coinmarketcap api, and resulted in a [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"My Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-02T17:54:31+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"headline\":\"How To Parse JSON With Excel VBA\",\"datePublished\":\"2020-12-02T17:54:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/\"},\"wordCount\":129,\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/JSON-PARSE-INPUT.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/\",\"name\":\"How To Parse JSON With Excel VBA - My Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/JSON-PARSE-INPUT.png\",\"datePublished\":\"2020-12-02T17:54:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/JSON-PARSE-INPUT.png\",\"contentUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/JSON-PARSE-INPUT.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/12\\\/02\\\/how-to-parse-json-with-excel-vba\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Parse JSON With Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\",\"name\":\"My Blog\",\"description\":\"My WordPress Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\\\/\\\/vbastring.com\\\/blog\"],\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Parse JSON With Excel VBA - My Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/","og_locale":"en_US","og_type":"article","og_title":"How To Parse JSON With Excel VBA - My Blog","og_description":"I didn&#8217;t come up with this code, but it is pretty cool. It shows how to parse a JSON string with VBA. The impetus for this was to access the price data for various coins on coinmarketcap.com &#8230;and to get the price data for 50+ coins required getting a coinmarketcap api, and resulted in a [&hellip;]","og_url":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/","og_site_name":"My Blog","article_published_time":"2020-12-02T17:54:31+00:00","og_image":[{"url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#article","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/"},"author":{"name":"admin","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"headline":"How To Parse JSON With Excel VBA","datePublished":"2020-12-02T17:54:31+00:00","mainEntityOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/"},"wordCount":129,"image":{"@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/","url":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/","name":"How To Parse JSON With Excel VBA - My Blog","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#primaryimage"},"image":{"@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png","datePublished":"2020-12-02T17:54:31+00:00","author":{"@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"breadcrumb":{"@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#primaryimage","url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png","contentUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/12\/JSON-PARSE-INPUT.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vbastring.com\/blog\/2020\/12\/02\/how-to-parse-json-with-excel-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vbastring.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Parse JSON With Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/vbastring.com\/blog\/#website","url":"https:\/\/vbastring.com\/blog\/","name":"My Blog","description":"My WordPress Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vbastring.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/vbastring.com\/blog"],"url":"https:\/\/vbastring.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/comments?post=950"}],"version-history":[{"count":5,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions"}],"predecessor-version":[{"id":961,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions\/961"}],"wp:attachment":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/media?parent=950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/categories?post=950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/tags?post=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}