0% found this document useful (0 votes)
142 views13 pages

Import Json To Excel and Export Excel To Json - Coding Is Love

json

Uploaded by

Jean Philippeaux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
142 views13 pages

Import Json To Excel and Export Excel To Json - Coding Is Love

json

Uploaded by

Jean Philippeaux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
61182018 Import Json to excel and export excel fo Json ~ Coding is Love fyv6 Coding is Love = wenu Project tracking, teamwork & client reporting like you've never seen before ads via Carbon Import Json to excel and export excel to Json & BYRANJITH KUMAR © JUNE 29, 2016 JSON (Javascript Object Notation) is the most used data exchange format nowadays. Microsoft Excel doesn’t have built-in support for importing JSON to excel or exporting excel data to JSON. VBA-JSON isan excellent library for parsing JSON in VBA. Lets see how to handle JSON in Excel VBA. Getting Started 1. Download VBA JSON latest version from here 2. Extract it, open VBA code editor in excel (Alt + F11) and import the library as shown in the gif below. hitpssicodingislove.comexcetjson! saa srn2018 Impor. 0 to excel ang export excel fo Json ~ Coding is Love 3, Add a reference to Microsoft scripting runtime. (Tools > references > select) Be clr ho sen Wea eas) fe mt Maw ene Format Dig Eun Tose Babine Vino Hop Do ya a RIBS > Olurca Import JSON to Excel This library provides a simple method parseason to parse JSON string into a dictionary object which can be used to extract data. Let's see an example. I'm using fake data from hitp://jsonplaceholder:typicode.com/ which is an API service with fake Json data. We'll be pulling user data from http://jsonplaceholder.typicode.com/users by making a GET request which responds with Json data. hitpssicodingislove.comexcetjson! anae 61182018 Import Json to excel and export excel to Json - Coding is Love 2" myeonne Grama”, 0 mao": ¢ . , : Nenana: eisrocraecsosa xe, Read more about GET requests in VBA here Next, we'll parse that Json and import it to excel. Code for importing data looks like this : view source print? 1 Public Sub exceljson() 2 Dim hetp As Onject, ISON As Object, 1 AS Integer 3 Set http = CreateObject( "HSXML2.XMLHTTP” ) 4 hetp.open "GET", “hetp://Jsonplaceholder. typicode.con/users” , False s http. Send 6 Set 3S0N = Parsedson(nttp.responseText) 7 i-2 hitpssicodingislove.comexcetjson! ana 61182018 18 n 2 a 14 a5 16 v 1 20 Import Json to excel and export excel to Json - Coding is Love For Each Iten In JSON Sheets(1).Cells(i, 1)-Value = Tten( "id" ) Sheets(1).Cells(i, 2).Value Teem( “name” ) Sheets(1).Cells(i, 3).Value = Ttem( “username” ) Sheets(1).Cells(1, 4).Value = Ttem( “email” ) sheets(1).Cells(1, 5).Value = Iten( "address" )( “city” ) Sheets(1).Cel1s(1, 6).Value = Item( “phone” ) Sheets(1).Cel1s(i, 7).Value = Item( “website” ) sheets(1).Cells(i, 8).Value = Iten( "company" )( “name” ) ieiea Next Msgdox ( “complete” ) End Sub Code explanation 1 First, define JSON as an object and make a GET request to JSON API hitpssicodingislove.comexcetjson! ana srn2018 Impor. 0 to excel ang export excel fo Json ~ Coding is Love 2. JSON data received in the response is parsed by passing it into Parseson method. 3. parsed data is converted into a collection of dictionaries. 4, Loop through the collection to get each user's details and set its values to the first sheet. Running above code looks like gif below. so x fm Rane onan ty ete Cony on } ctnonote 3} ‘ | & "| Reading JSON from a file In the same example above, If you want to read JSON data froma local file then you can use Filesystenobject to read all text in the file and then pass it to ParseJson method. view source print? 1 Dim FSO As New Falesystendbject 2 Dim JsonTS As TextStream 3 Set IsonTS = FS0.OpenTextFile( “exanple.json” , ForReading) hitpssicodingislove.comexcetjson! sae 61182018 Import Json to excel and export excel fo Json - Coding is Love JsonText = JsonTs.ReadAll dsonTs.close Set ISON = ParseJson(JsonText) Export Excel to Json VBA-JSON provides another method convertto3son which can be used to convert excel data into JSON. Here's an example, Sample data with Name, Phone and Email is present in second sheet. Let's convert it into JSON Code for this looks like : view source print? Public Sub exceltojson() Dim rng As Range, itens As New Collection, nyiter As New Dictionary, & As Integer, cell As Variant set rng = Range( "A2:A3" ) "Set rng = Range(Sheets(2)-Range( "A2" ), Sheets(2).Range( "A2" ).End(x1Down)) use this for dynamic range For Each cell In rng hitpssicodingislove.comexcetjson! ona 61182018 Import Json to excel and export excel fo Json - Coding is Love 7 Debug. Print (cell.Value) a nyitem( “name” ) = cell.value 9 myiten( “email” ) = cell.offset(@, 1).Value 10 nyitem( “phone” ) = cell.offset(@, 2).Value n itens.Add ayitem R Set nyitem = Nothing a dedea 14 Next 15 Sheets(1)-Range( "Ad" ).Value = ConvertTo3son(itens, whitespace: 16 End Sub Code Explanation 1. First, define rng as range and set it to data range. 2. convertroason method takes a dictionary collection or array as parameter. So we should pass our data as a collection. 3. A Dictionary is an object with keys and values just like JSON but doesn’t support multiple items like arrays or collections, so we create a dictionary for each item and push it into an array or a collection. 4, Define a dictionary and a collection, loop through the range and set each row's data into ayizen 5, Push nyiten into collection and set to nothing, because we are u the same dictionary to add next row’s data and push it to collection hitpssicodingislove.comexcetjson! mae 61182018 hitpssicodingislove.comexcetjson! Import Json to excel and export excel fo Json - Coding is Love again Finally pass stens collection to converttozson method which returns a JSON string. Running above code looks like gif below vee ata Tee Co SS Sianititte. |" & Dea SSS Ce Bima Bee a fe 2 mie owaesesoen yettoen Export Excel to JSON file In the same example above, If you want to export excel data to JSON file then It can be done by opening a file for output by specifying the path of the file and printing data in it. Sample code below, Running this would save a JSON file in the current workbook’s folder. view source print? 1 Public Sub exceltojsonfile() 2 Dim rng As Range, items As New Collection, myiten As New Dictionary, i As Integer, cell As Variant, myfile As String ana 61182018 18 n 2 B 4 15 16 Import Json to excel and export excel fo Json - Coding is Love Set rng = Range( "A2:A3" ) “Set rng = Range(Sheets(2) .Range( 2"), Sheets(2).Range( "A2” ).End(x1Down)) use this for dynamic range For Each cell Tn rng Debug. Print (cell.value) nyitem( “name” ) = cell.value nyiten( “enail" ) = cell.offset(@, 1).Value myitem( "phone" ) = cell.offset(a, 2).value items.Add myiten Set nyiten = Nothing Next nyfile = Application.ActiveWorkbook.Path & "\data. json” Open ayfile For Output As #2 hitpssicodingislove.comexcetjson! ona 61182018 Import Json to excel and export excel fo Json - Coding is Love 17 Print #1, ConvertTodson(itens, Whitespace:-2) 18 Close #2 1 End Sub Export Excel to Nested JSON Above code can be modified a bit to get a nested JSON as output. Just add dictionary in another dictionary so that it creates a nested JSON. code looks like this : view source print? 1 Public Sub exceltonestedjson() 2 Dim rng As Range, items As New Collection, myitem As New Dictionary, subitem As New Dictionary, 1 As Integer, cell As Variant 3 Set rng = Range( "A2:A3" ) 4 "Set rng = Range(Sheets(2).Range( "A2" ), Sheets (2).Range( "A2" ).End(xlDown)) use this for dynamic range 6 For Each cell In rng 7 Debug.Print (cell.value) 8 nyitem( “name” ) = cell.value hitpssicodingislove.comexcetjson! rona8 61182018 18 u 2 B “4 1s 16 v Import Json to excel and export excel fo Json - Coding is Love nyiten( “enail" ) = cell.offset(@, 1).Value myitem( "phone" ) = cell.offset(a, 2).value subitem( "country ) = cell.offset(@, 3).Value nyiten.Adé “location” , subiten items.Add ayiten Set nyitem = Nothing Set subiten Nothing. Next 18 Sheets(2)-Range( "Ad" ).Value = ConvertToJson(items, Whitespace:=2) 19 Run hitpssicodingislove.comexcetjson! End Sub ig above code looks like image below ana 61182018 Import Json to excel and export excel fo Json - Coding is Love = Fl eae a 2 Iaith ‘snitkamar2s2anal com 2 [Ron ‘emanngoamalon Wrapping up Read official documentation of VBA-JSON here and use VBA-Dictionary for Mac Support. Related articles : + Complete JSON tutorial here - JSON for beginners © Handling CSV in VBA. If you have any questions or feedback, comment below and please use CodingisLove Bin for sharing your code. Get notified when there's a new post by clicking on @) in bottom left Need some help? Post your questions on our forum hitpssicodingislove.comexcetjson! 61182018 Import Json to excel and export excel fo Json - Coding is Love Your Reaction? 124 1" 19 o 6 ‘Awesome Love Like Dislike Bad 151 Shar Shai Related Dale oe — — JSON Master: In-depth Convert Jsontocsvand —_XmilHttpRequest - Http JSON tutorial for csv to Jsonin Excel VBA requests in Excel VBA Beginners October 25, 2¢ jt 016 January 21, 20 In "Coding Tutorials’ in "Coding Tutorials In "Coding Tutorials’ POSTED IN CODING TUTORIALS * TAGGED EXCEL, VBA Author: Ranjith kumar ij ACA student by education, self taught coder by passion, loves to explore new technologies and believes in learn by doing, View all posts by Ranjith kumar PREV NEXT XmlHttpRequest - Http requests How to place a fixed button at in Excel VBA bottom right of the screen in html 386 Thoughts hitpssicodingislove.comexcetjson! aia8

You might also like