Parse JSON With Excel VBA
Parse JSON With Excel VBA
Parse JSON With Excel VBA
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"); } }
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.
Thread Tools Show Printable Version Subscribe to this Thread Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode
Ferdi24 View Profile View Forum Posts Private Message New Member Join Date Feb 2012Posts 19
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
Juan Pablo Gonzlez View Profile View Forum Posts Visit Homepage MrExcel MVP Join Date Feb 2002Location Bogota, ColombiaPosts 11,942
Ferdi24 View Profile View Forum Posts Private Message New Member Join Date Feb 2012Posts 19
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?
Ferdi24 View Profile View Forum Posts Private Message New Member Join Date Feb 2012Posts 19
Or maybe anyone else would have any additional ideas? Or could help me out with what Juan suggested?
Juan Pablo Gonzlez View Profile View Forum Posts Visit Homepage MrExcel MVP Join Date Feb 2002Location Bogota, ColombiaPosts 11,942
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
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!
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
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.