Parse JSON With Excel VBA

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The document discusses parsing JSON in Excel VBA using the VBJSON library and writing the parsed data to an Excel sheet in table format.

The VBJSON library needs to be referenced and the JSON text needs to be wrapped in curly braces to make it a valid JSON object before parsing. The parsed data is then accessed via keys and collections.

The JSON.bas, cJSONScript.cls and cStringBuilder.cls need to be imported. References added to Scripting and ADO libraries. The JSON text is parsed using JSON.parse and the rows are looped through to extract the values and write to the sheet.

<!-- if (typeof YAHOO === 'undefined') // Load ALL YUI Local { document.write('<script type="text/javascript" src="clientscript/yui/yuiloader-dom-event/yuiloader-dom-event.js?v=420"><\/scrip t>'); document.

write('<script type="text/javascript" src="clientscript/yui/connection/connection-min.js?v=420"><\/script>'); var yuipath = 'clientscript/yui'; var yuicombopath = ''; var remoteyui = false; } else // Load Rest of YUI remotely (where possible) { var yuipath = 'clientscript/yui'; var yuicombopath = ''; var remoteyui = true; if (!yuicombopath) { document.write('<script type="text/javascript" src="clientscript/yui/connection/connection-min.js"><\/script>'); } } var SESSIONURL = "s=c3de6c7920a9aa25efdc19cf1efcc86e&"; var SECURITYTOKEN = "guest"; var IMGDIR_MISC = "images/misc"; var IMGDIR_BUTTON = "images/buttons"; var vb_disable_ajax = parseInt("0", 10); var SIMPLEVERSION = "420"; var BBURL = "http://www.mrexcel.com/forum"; var LOGGEDIN = 0 > 0 ? true : false; var THIS_SCRIPT = "showthread"; var RELPATH = "showthread.php?640780-Parse-JSON-with-Excel-VBA"; var PATHS = { forum : "", cms : "", blog : "" }; var AJAXBASEURL = "http://www.mrexcel.com/forum/"; // --> P arse JSON with Excel VBA

Register Help

Remember Me? YAHOO.util.Dom.setStyle('navbar_password_hint', "display", "inline"); YAHOO.util.Dom.setStyle('navbar_password', "display", "none"); vB_XHTML_Ready.subscribe(function() { // YAHOO.util.Event.on('navbar_username', "focus", navbar_username_focus); YAHOO.util.Event.on('navbar_username', "blur", navbar_username_blur); YAHOO.util.Event.on('navbar_password_hint', "focus", navbar_password_hint); YAHOO.util.Event.on('navbar_password', "blur", navbar_password); }); function navbar_username_focus(e) { // var textbox = YAHOO.util.Event.getTarget(e); if (textbox.value == 'User Name') { // textbox.value=''; textbox.style.color='#000000'; } } function navbar_username_blur(e) { // var textbox = YAHOO.util.Event.getTarget(e); if (textbox.value == '') { // textbox.value='User Name'; textbox.style.color='#777777'; } } function navbar_password_hint(e) { // var textbox = YAHOO.util.Event.getTarget(e); YAHOO.util.Dom.setStyle('navbar_password_hint', "display", "none"); YAHOO.util.Dom.setStyle('navbar_password', "display", "inline"); YAHOO.util.Dom.get('navbar_password').focus(); } function navbar_password(e) { // var textbox = YAHOO.util.Event.getTarget(e); if (textbox.value == '') { YAHOO.util.Dom.setStyle('navbar_password_hint', "display", "inline"); YAHOO.util.Dom.setStyle('navbar_password', "display", "none"); } }

ForumFAQ Forum ActionsMark Forums Read

Quick LinksToday's Posts Search New Posts Zero Reply Posts Subscribed Threads

Advanced Search

Forum Question Forums Excel Questions Parse JSON with Excel VBA

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Results 1 to 6 of 6 Thread: Parse JSON with Excel VBA

Thread Tools Show Printable Version Subscribe to this Thread Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode

Jun 14th, 2012, 04:38 PM#1

Ferdi24 View Profile View Forum Posts Private Message New Member Join Date Feb 2012Posts 19

Parse JSON with Excel VBA

Hi Experts, I'm trying to parse a JSON string in Excel VBA into an array so that I can write it in table-format into a sheet. Example: I have this JSON string in cell A1 "rows":[["20120604", "ABC", "89"],["20120604", "BCD", "120"],["20120604", "CDE","239"]] The final result in my sheet should look like this: 20120604 | ABC | 89 20120604 | BCD | 20 20120604 | CDE | 239 The format of the content inbetween the [ ] could be anything. Example 1: "rows":[["20120604","122"],["20120604","239"],["20120604","150"]] Example2: "rows":[["ABC","34"],["BCD","111"],["CDE","459"]] Example3: "rows":[["20120604", "ABC", "89"],["20120604", "BCD", "120"],["20120604", "CDE","239"]] Example4: "rows":[["XYZ", "ABC", "89","15"],["WXY", "BCD", "120","11"],["VWX", "CDE","239","19"]] Does anyone have any ideas and would be willing to share his/her wisdom ith me? Thanks a lot for reading

Reply With Quote

Jun 14th, 2012, 04:48 PM#2

Juan Pablo Gonzlez View Profile View Forum Posts Visit Homepage MrExcel MVP Join Date Feb 2002Location Bogota, ColombiaPosts 11,942

Re: Parse JSON with Excel VBA

I'm using this: http://www.ediy.co.nz/vbjson-json-pa...xidc55680.html And has been working great.

Regards, Juan Pablo Gonzlez http://www.juanpg.com

Reply With Quote

Jun 14th, 2012, 04:59 PM#3

Ferdi24 View Profile View Forum Posts Private Message New Member Join Date Feb 2012Posts 19

Re: Parse JSON with Excel VBA

Hi Juan, Thanks for the link. I have found this before, but I am not sure how I can ues this to convert JSON to the desired format of a table. Do you know if there is a tutorial or s.th. avaiable around this? Or if it's not too complicated to explain, do you have any hint what exactly I would need to do?

Reply With Quote

Jun 15th, 2012, 05:40 AM#4

Ferdi24 View Profile View Forum Posts Private Message New Member Join Date Feb 2012Posts 19

Re: Parse JSON with Excel VBA

Or maybe anyone else would have any additional ideas? Or could help me out with what Juan suggested?

Reply With Quote

Jun 15th, 2012, 09:35 AM#5

Juan Pablo Gonzlez View Profile View Forum Posts Visit Homepage MrExcel MVP Join Date Feb 2002Location Bogota, ColombiaPosts 11,942

Re: Parse JSON with Excel VBA

You need to import the "JSON.bas", "cJSONScript.cls" and "cStringBuilder.cls" to your project. Go to Tools | References and add a reference to the "Microsoft Scripting Runtime" library and to the "Microsoft ActiveX Data Objects 2.8 Library" library. Now, the text that you have in A1 is not a real JSON object, so we need to trick it in the code a little bit (By enclosing it in {} ).

Code: Sub Test() Dim jsonText As String Dim jsonObj As Dictionary Dim jsonRows As Collection Dim jsonRow As Collection Dim ws As Worksheet Dim currentRow As Long Dim startColumn As Long Dim i As Long Set ws = Worksheets("Sheet1") 'Create a real JSON object jsonText = "{" & ws.Range("A1").Value & "}" 'Parse it Set jsonObj = JSON.parse(jsonText) 'Get the rows collection Set jsonRows = jsonObj("rows") 'Set the starting row where to put the values currentRow = 1 'First column where to put the values startColumn = 2 'B 'Loop through all the values received For Each jsonRow In jsonRows 'Now loop through all the items in this row For i = 1 To jsonRow.Count ws.Cells(currentRow, startColumn + i - 1).Value = jsonRow(i) Next i 'Increment the row to the next one currentRow = currentRow + 1 Next jsonRow End Sub Regards, Juan Pablo Gonzlez http://www.juanpg.com

Reply With Quote

Jun 15th, 2012, 04:16 PM#6

Ferdi24 View Profile View Forum Posts

Private Message New Member Join Date Feb 2012Posts 19

Re: Parse JSON with Excel VBA

That works brilliantly, Juan, you cannot imagine how happy I am with this. And it is so simple! You have just ended my 2 week long project. Thank you very much, it's nice to find people on the internet willing to help others. I appreciate you taking the time. Hopefully this can also be a reference for others, too, as I was really struggling to find something people like me can understand. Thanks!

Reply With Quote

Quick NavigationExcel QuestionsTopSite Areas Settings Private Messages Subscriptions Who's Online Search Forums Forums Home Forums AnnouncementsBoard Announcements Question ForumsExcel Questions Questions in Other Languages PowerPivot Questions Microsoft Access General Excel Discussion & Other Questions MrExcel ProductsMrExcel's Products: Books, CDs, Podcasts Easy-XL Add-In The LoungeLounge v.2.0 Other ForumsAbout This Board Hall of Fame Winners

Test Here

Previous Thread | Next Thread

Bookmarks Bookmarks Digg del.icio.us StumbleUpon Google Posting Permissions You You You You may may may may not not not not post post post edit new threads replies attachments your posts

BB code is On Smilies are On [IMG] code is On [VIDEO] code is On HTML code is On Forum Rules -- vB4 Default Style-- Default Mobile Style Contact Us Ask Mr. Excel - Tips and Solutions for Excel Archive Privacy Statement Terms of Service Top <!-- // Main vBulletin Javascript Initialization vBulletin_init(); //--> All times are GMT -4. The time now is 10:20 PM. Powered by vBulletin Version 4.2.0 Copyright 2012 vBulletin Solutions, Inc. All rights reserved. All contents Copyright 1998-2012 by MrExcel Consulting.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy