To Parse Custom JSON data is to split out its name/value pairs into a more readable useable format.
Excel is a powerful tool that allows you to connect to JSON data and read it. However sometimes this data might require a little manipulation to be fully understood and analysed in Excel.
In this article you will learn
- What is JSON Data
- To Connect to JSON data from Excel
- How to Parse simple JSON Data using Excels Power Query
- To Parse complex JSON Data using Excels Power Query
This article contains data tables to download so you can practice along and master the art of parsing custom JSON data using Excel. We are also powered with STEEM so you can earn while you learn . If you are not familiar with Excel Power Tools you can find out about them here.
What is JSON Data?
JSON data is a way of representing objects or arrays. It is easy to read, and it is easy to parse, even with Excel. Many API calls will return JSON format and many web apps use JSON which easily moves information around the internet.
The syntax for JSON comes from JavaScript and can be summarized as follows:
- Data is shown in name/value pairs separated by : For example “name” : ”paulag”
- Data is separated by commas. For Example “name” : ”paulag” , “Sex” : “Female”
- Curly brackets represent an object. An object being a name/value pair separated by a comma. For Example {“name” : ”paulag” , “Sex” : “Female”}
- Square brackets hold arrays and contains a list of values separated by a comma.
Look at the sample JSON in the image below
{“total_population”: This shows the first object, which is a name/value pair. The name of the object is total_population
[{“date”: “2019-01-02”, “population”: 7638335801}, {“date”: “2019-01-03”, “population”: 7638557789}]} This is the value for the total population. The [ represents an array. This array contains two objects. The objects are defined within the curly brackets and separated with a comma. Each object contains 2 lots of data (name/value pair) also separated with a comma. The data, shown in the name/value pairs, in this example is date and population.
Here is one you can look at yourself. https://api.coinmarketcap.com/v1/ticker/bitcoin/
If you enter this to your browser, you will get something like the below:
Let’s take a look at that image in more detail.
Connecting to JSON data from Excel
In Excels Data ribbon, under GET and Transform Data, we have the option of connecting to data of multiple sources and multiple types.
If we select Get data from file, we will then have the option to get data from a JSON file.
In this example we have URL API endpoint https://api.coinmarketcap.com/v1/ticker/bitcoin/. Therefore, from the Data Ribbon we can select, Get data from Web. This will open a dialogue box in which you place the URL.
Next, Power query will then open. Power query is a magic excel tool that will allow you transform data that you connect to into a usable format.
The JSON data will appear as a list of records Power Query. For excel to read this, we must convert a list to a table. Select ‘to table’ from the available option.
Next, Power query will create a table and you will see this step appear on the right of the power query window under applied steps.
This new table contains a record. We must expand this record to get the value pairs. As this record only has 1 row, we would expect this to expand across the columns. To do this, right click on the arrows in the column header.
This will reveal the names of the value pairs. By selecting ok a new column will be set up in the table. The name will be in the header and the value in the row.
Further transformations
If we wanted this data going down the row, we could Unpivot the columns. By selecting the id column. Then from the Transform ribbon select the dropdown for unpivot columns and select unpivot other columns.
When working with Power Query, it’s important to make sure you have the correct data types set. To work with this data, we must now move to from Power Query to Excel. If we select File, and then select Close and load, this will load the data as a table in Excel. Or, if we select or Close and Load to, the data will be loaded into a Power Pivot Model.
How to Parse JSON Data in Excel
Very often when you access JSON data with Excel it appears in 1 column. This can happen for many reasons and is often the design of a database.
Look at the image below. We can see the json_metadata field is still in its JSON syntax
When we encounter data like this, we can easily parse the column into its components. From the image below we see we have 4 components. We have An Array, an Object, the data, and one of the data fields contains an array.
STEP by STEP
Download this file. It contains a table as shown below. (do not copy and paste the table as the JSON field will not be recognised.)
tx_id |
tid |
json_metadata |
timestamp |
647524676 |
follow |
[“follow”,{“follower”:”mervin-gil”,”following”:”jarvie”,”what”:[“blog”]}] |
43466.89097 |
647524682 |
follow |
[“follow”,{“follower”:”steliosfan”,”following”:”michealb”,”what”:[“blog”]}] |
43466.89097 |
647524833 |
follow |
[“follow”,{“follower”:”eugenezh”,”following”:”zentricbau”,”what”:[“blog”]}] |
43466.89097 |
647524855 |
follow |
[“follow”,{“follower”:”bitcoinportugal”,”following”:”manuellevi”,”what”:[]}] |
43466.89097 |
647525074 |
follow |
[“follow”,{“follower”:”eugenezh”,”following”:”adriellute”,”what”:[“blog”]}] |
43466.89167 |
647525089 |
follow |
[“follow”,{“follower”:”bigbigtoe”,”following”:”hoxly”,”what”:[“blog”]}] |
43466.89167 |
647525121 |
follow |
[“follow”,{“follower”:”mervin-gil”,”following”:”loveon”,”what”:[“blog”]}] |
43466.89167 |
647525159 |
follow |
[“follow”,{“follower”:”mervin-gil”,”following”:”pechichemena”,”what”:[“blog”]}] |
43466.89167 |
647525233 |
follow |
[“follow”,{“follower”:”imealien”,”following”:”pataty69″,”what”:[“blog”]}] |
43466.89167 |
647525652 |
follow |
[“follow”,{“follower”:”mervin-gil”,”following”:”kamile”,”what”:[“blog”]}] |
43466.89236 |
647525818 |
follow |
[“follow”,{“follower”:”bitcoinportugal”,”following”:”drmake”,”what”:[“blog”]}] |
43466.89236 |
647525886 |
follow |
[“follow”,{“follower”:”mervin-gil”,”following”:”bradfordtennyson”,”what”:[“blog”]}] |
43466.89236 |
647525980 |
follow |
[“follow”,{“follower”:”a0i”,”following”:”shoemanchu”,”what”:[“blog”]}] |
43466.89236 |
647526007 |
follow |
[“follow”,{“follower”:”voteme”,”following”:”kostyantin”,”what”:[“blog”]}] |
43466.89236 |
648215552 |
follow |
[“follow”,{“follower”:”ansie”,”following”:”hoxly”,”what”:[“blog”]}] |
43467.75833 |
648215582 |
follow |
[“follow”,{“follower”:”ashokcan143″,”following”:”abcor”,”what”:[]}] |
43467.75833 |
648215691 |
follow |
[“follow”,{“follower”:”ashokcan143″,”following”:”abasinkanga”,”what”:[]}] |
43467.75903 |
648215820 |
follow |
[“follow”,{“follower”:”nongvo.piy”,”following”:”acidyo”,”what”:[]}] |
43467.75903 |
648215859 |
follow |
[“follow”,{“follower”:”grid9games”,”following”:”yeninsfer”,”what”:[“blog”]}] |
43467.75903 |
648215945 |
follow |
[“follow”,{“follower”:”nongvo.piy”,”following”:”acidyo”,”what”:[“blog”]}] |
43467.75903 |
Click on any of the cells that contain the data and from the Data Ribbon select FROM Table/Range
If your data is not in table format, Excel will then prompt you to create a table. Select the cells the contain the data and tick the box to say that your table has headers.
Power Query editor window will open. On the canvas you can see your data and, on the right, you can see any transformation steps that have taken place.
To parse the json column, first select the column, then on the Transform Ribbon select Parse and select JSON
Power query will recognise the first [ and create a list format. Next, we need to expand this list to new rows. To do this click on the arrows on the top of the column and select Expand to New Rows.
What is returned is two lines for each tx-id. The json column now has a row for the name of the array, which is follow, and a record. The record will contain the data.
As we do not need the name of the array, we can use the filter to remove all the follow rows
We are now left with the records. We can expand this record, by pressing the arrow on the column. From here we see we have the names of 3 data fields, Follower, Following and What
When we select OK, we get a new column in our data table for each data field.
However, the field named what contains an array, which is again shown as a list (or array) which needs to be expanded
Once we expand this, we are at the end of the JSON data and have extracted the relevant columns. You can now use this data for further analysis in Excel or Power Pivot. To load it back to excel, select File and Close & Load.
More complex JSON data extractions in Excel
So far, we have looked at getting JSON data into Power Query using an Excel table and directly from a URL. There are other ways you can connect to JSON data including connecting directly to a JSON file. Connecting is the easy part. Things get more complicated when you have JSON columns where the strings are different in each row. One might start with an array and so return a list, but some might start with an object and return a record.
In Power Query lists are expanded to new rows in the table and records are expanded to new columns. Parsing custom JSON data in Excel can require some thinking.
You can parse (or deconstruct) the contents of a column with text strings that contain JSON or XML. To help illustrate how to use the Parse command, the following sample data starts with JSON and XML data entered as text fields.
For more information on importing JSON and XML, see Import data from external data sources.
JavaScript Object Notation (JSON) is a common data format, and you can import it into Excel.
To transform the SalesPerson column from text strings to a structured Record column:
-
Select the SalesPerson column.
-
Select Transform > Parse > JSON.
-
Select Record to see the values.
-
Select the Expand icon next to the SalesPerson column header. From the Expand columns dialog box, select only the FirstName and LastName fields.
Result
Extensible Markup Language (XML) is a common markup and data format, and you can import it into Excel.
To transform the Country column from text strings to Table values.
-
Select the Country column.
-
Select Transform > Parse > XML.
-
Select Table to see the values.
-
Select the Expand icon next to the Country column header. From the Expand columns dialog box, select only the Country and Division fields.
Result
Tip With a Table, you can also select Aggregate to aggregate data in different ways, such as Sum or Count. For more information, see Aggregate data from a column.
See Also
Power Query for Excel Help
Work with a List, Record, or Table structured column
Parse text as JSON or XML (docs.com)
Need more help?
Want more options?
Explore subscription benefits, browse training courses, learn how to secure your device, and more.
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
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 is an excellent library for parsing JSON in VBA. Lets see how to handle JSON in Excel VBA. If you’re new to JSON then read JSON tutorial for beginners
Prerequisites
- Save your excel file as Macro-Enabled workbook (Refer screen shot below)
- Enable macros if they are not already enabled. You can enable it by cliking on file > options > Trust Center > Trust Center Settings > Enable all macros
Getting Started
- Download VBA JSON latest version from here
- Extract it, open VBA code editor in excel (Alt + F11) and import the library as shown in the gif below.
- Add a reference to Microsoft scripting runtime. (Tools > references > select)
-
Add a reference to MSXML (Tools > references)
Select appropriate version based on your PC :
1. Microsoft XML, v 3.0.
2. Microsoft XML, v 4.0 (if you have installed MSXML 4.0 separately).
3. Microsoft XML, v 5.0 (if you have installed Office 2003 – 2007 which provides MSXML 5.0 for Microsoft Office Applications).
4. Microsoft XML, v 6.0 for latest versions of MS Office.
Import JSON to Excel
This library provides a simple method ParseJson
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 http://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.
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 :
Public Sub exceljson() Dim http As Object, JSON As Object, i As Integer Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", "http://jsonplaceholder.typicode.com/users", False http.Send Set JSON = ParseJson(http.responseText) i = 2 For Each Item In JSON Sheets(1).Cells(i, 1).Value = Item("id") Sheets(1).Cells(i, 2).Value = Item("name") Sheets(1).Cells(i, 3).Value = Item("username") Sheets(1).Cells(i, 4).Value = Item("email") Sheets(1).Cells(i, 5).Value = Item("address")("city") Sheets(1).Cells(i, 6).Value = Item("phone") Sheets(1).Cells(i, 7).Value = Item("website") Sheets(1).Cells(i, 8).Value = Item("company")("name") i = i + 1 Next MsgBox ("complete") End Sub
Code explanation
- First, define JSON as an object and make a GET request to JSON API
- JSON data received in the response is parsed by passing it into
ParseJson
method. - parsed data is converted into a collection of dictionaries.
- 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.
Reading JSON from a file
In the same example above, If you want to read JSON data from a local file then you can use FileSystemObject
to read all text in the file and then pass it to ParseJson
method.
Dim FSO As New FileSystemObject Dim JsonTS As TextStream Set JsonTS = FSO.OpenTextFile("example.json", ForReading) JsonText = JsonTS.ReadAll JsonTS.Close Set JSON = ParseJson(JsonText)
Export Excel to Json
VBA-JSON provides another method ConvertToJson
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 :
Public Sub exceltojson() Dim rng As Range, items As New Collection, myitem As New Dictionary, i As Integer, cell As Variant set rng = Range("A2:A3") 'Set rng = Range(Sheets(2).Range("A2"), Sheets(2).Range("A2").End(xlDown)) use this for dynamic range i = 0 For Each cell In rng Debug.Print (cell.Value) myitem("name") = cell.Value myitem("email") = cell.Offset(0, 1).Value myitem("phone") = cell.Offset(0, 2).Value items.Add myitem Set myitem = Nothing i = i + 1 Next Sheets(2).Range("A4").Value = ConvertToJson(items, Whitespace:=2) End Sub
Code Explanation
- First, define
rng
as range and set it to data range. ConvertToJson
method takes a dictionary collection or array as parameter. So we should pass our data as a collection.- 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.
- Define a dictionary and a collection, loop through the range and set each row’s data into
myitem
- Push
myitem
into collection and set it to nothing, because we are using the same dictionary to add next row’s data and push it to collection again.
Finally pass items
collection to ConvertToJson
method which returns a JSON string.
Running above code looks like gif below
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.
Public Sub exceltojsonfile() Dim rng As Range, items As New Collection, myitem As New Dictionary, i As Integer, cell As Variant, myfile As String Set rng = Range("A2:A3") 'Set rng = Range(Sheets(2).Range("A2"), Sheets(2).Range("A2").End(xlDown)) use this for dynamic range i = 0 For Each cell In rng Debug.Print (cell.Value) myitem("name") = cell.Value myitem("email") = cell.Offset(0, 1).Value myitem("phone") = cell.Offset(0, 2).Value items.Add myitem Set myitem = Nothing i = i + 1 Next myfile = Application.ActiveWorkbook.Path & "data.json" Open myfile For Output As #1 Print #1, ConvertToJson(items, Whitespace:=2) Close #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 :
Public Sub exceltonestedjson() Dim rng As Range, items As New Collection, myitem As New Dictionary, subitem As New Dictionary, i As Integer, cell As Variant Set rng = Range("A2:A3") 'Set rng = Range(Sheets(2).Range("A2"), Sheets(2).Range("A2").End(xlDown)) use this for dynamic range i = 0 For Each cell In rng Debug.Print (cell.Value) myitem("name") = cell.Value myitem("email") = cell.Offset(0, 1).Value myitem("phone") = cell.Offset(0, 2).Value subitem("country") = cell.Offset(0, 3).Value myitem.Add "location", subitem items.Add myitem Set myitem = Nothing Set subitem = Nothing i = i + 1 Next Sheets(2).Range("A4").Value = ConvertToJson(items, Whitespace:=2) End Sub
Running above code looks like image below
Using array of strings and objects in JSON
One of our readers asked me how to use an array of strings and objects inside the JSON.
Here’s how to do it:
Public Sub exceltojson() Dim rng As Range, items As New Collection, myitem As New Dictionary, i As Integer, cell As Variant, objectContainer As New Dictionary, arrayContainer As New Collection Dim j As Integer 'Set rng = Range("A2:A3") Set rng = Range(Sheets(1).Range("A2"), Sheets(1).Range("A2").End(xlDown)) 'use this for dynamic range i = 0 For Each cell In rng myitem("id") = cell.Value myitem("name") = cell.Offset(0, 1).Value 'tags tagsString = cell.Offset(0, 2).Value tagsCollection = getCollectionFromString(tagsString) myitem.Add "tags", tagsCollection 'ingredients ingredientsString = cell.Offset(0, 3).Value weightsString = cell.Offset(0, 4).Value ingredientsUnit = cell.Offset(0, 5).Value ingredientsCollection = getCollectionFromString(ingredientsString) weightsCollection = getCollectionFromString(weightsString) j = 0 For Each ingredient In ingredientsCollection objectContainer("ingredientnaam") = ingredient objectContainer("eenheid") = ingredientsUnit objectContainer("hoeveelheid") = weightsCollection(j) arrayContainer.Add objectContainer Set objectContainer = Nothing j = j + 1 Next myitem.Add "ingredienten", arrayContainer 'Reset values Set arrayContainer = Nothing j = 0 items.Add myitem Set myitem = Nothing i = i + 1 Next Sheets(1).Range("A6").Value = ConvertToJson(items, Whitespace:=2) End Sub Function getCollectionFromString(val) getCollectionFromString = Split(val, ", ") End Function
Running above code looks like image below
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.
- Author
- Recent Posts
A CA- by education, self taught coder by passion, loves to explore new technologies and believes in learn by doing.
16 Aug How to Import JSON to Excel Using VBA
Posted at 13:26h
in Excel VBA
12 Comments
Howdee! It’s becoming increasingly more common for data to be generated in a JSON format as opposed to XML. XML was widely used for years, but recently JSON has started to become the preferred method of data exchange. Many REST APIs have the option to return both but the majority that I interact with default to returning JSON formatted data. Therefore, as excel ninjas, it’s important to understand how to import JSON to Excel for analysis. Before we begin on that route, let’s take a quick moment to talk about what JSON is for those that aren’t familiar with it.
Curious how to do this in VSTO? Click Here!
What is JSON?
JSON stands for JavaScript Object Notation and is a lightweight data-interchange format. In layman’s terms, it is a string of text that represents a universal data structure. It is easy for humans to read (when formatted properly) and, because of the universal structure, it is very easy and fast for machines to parse and generate. It’s made simple because JSON, at it’s most basic, is only two data types. It is made up of objects, or arrays, or a combination of both. Objects are made up of key/value pairs (often called a dictionary) and arrays are simply collections of values or objects separated by a comma.
It’s important to note that object/dictionary and array are the terminology applicable to the .NET language. Other languages might have other terminology such as hash table for object, or vector for an array. This site gives a great high level, cross-language overview of JSON.
JSON Object
A JSON object always begins with { and ends with }. The key is separated from its value with a colon (“:”). The key/value pairs will then be separated by commas all within the curly brackets. In the image below, the first key “color” corresponds to its value “red”, while the second key “value” corresponds to the red hex code “#f00”. Hopefully you can see why this might be called a dictionary as you look up the key (word to define) to get its value (definition of the word).
JSON Array
A JSON array, in its simplest form, is a list of values separated by a comma that begins with [ and ends with ]. In the below image, the JSON array is a list of string data points. We can see that they are colors but there is no key in this example explicitly telling that this string represents a color. Therefore, JSON is most commonly seen as a combination of arrays and objects.
JSON Objects & Arrays Together
Most of the time, JSON is returned as an array of objects. If you recall, an object always begins and ends with curly brackets. Therefore, the array of objects would have several objects enclosed in curly brackets that are separated by commas and all objects are enclosed in normal brackets (the array). That looks like this image:
In this example, each object represents a color and its corresponding hex code. In Excel, this would likely be two columns of data. One column for color, and the other for its hex code value. Now that you’re more familiar with JSON data structure, let’s learn how to import JSON to Excel!
Setup JSON to Excel
There are several libraries out there to help you import JSON to Excel. The most popular of these is VBA-JSON which is available here on GitHub. This library will do most of the hard work of parsing the JSON string to a dictionary for you and then you can write the values to an excel worksheet. When you download the latest release, you’ll need to import the “JsonConverter.bas” file to your VBA project. This GIF shows how that is accomplished. You will also need to add a reference to the “Microsoft Scripting Runtime” by clicking tools from the ribbon, selecting references and checking the box for Microsoft Scripting Runtime as shown in the screen grab.
Once you have completed both steps, insert a new module into your project and title it whatever you like. To get started, we need to dimension some variables to use later.
Dim ws As Worksheet
Dim jsonText As String
Dim jsonObject As Object
The ws variable will represent a worksheet in excel. In this example, we will use this worksheet to both read in our JSON string from and write the parsed JSON to Excel. To assign a worksheet to this variable, set it like this:
Set ws = Worksheets("JSON to Excel Example")
The jsonText variable will represent our string of valid JSON data. For this example, I’ve pasted that string of colors and hex codes we looked at earlier into cell A1 on the JSON to Excel Example tab. To assign that string to a variable, type this code:
jsonText = ws.Cells(1, 1)
This step is not necessary for the code to work for us. We could simply reference the cell in the worksheet that contains the JSON. However, most of the time you’ll be returning your JSON string from another data source, most likely a HTTP GET Web Call. If you’re not familiar with HTTP Requests in VBA, click here to learn more.
Lastly, to put the JSON string into the jsonObject, we will use one of the methods contained in the JsonConverter file you imported to begin this example. Since the builder of this library made all of the subroutines public, it is callable from the module this code is in. That call looks like this:
Set jsonObject = JsonConverter.ParseJson(jsonText)
This call instructs the JsonConverter module to parse the JSON string you’ve passed it using the jsonText variable to a collection of dictionaries that we can now access. Now that we’ve finished our setup, we can start learning to import JSON to Excel in the next section.
Import JSON to Excel
The code to import JSON to Excel might sound daunting, but it is much easier than it sounds. At a high level, all you have to do is loop through the jsonObject you’ve created, and write each dictionary value to the sheet. The wonderful thing about dictionaries, is this is easy to do with a for each loop. I’ve added a couple of variables and a for each loop to our code and the result is this:
Sub JsonToExcelExample()
Dim jsonText As String
Dim jsonObject As Object, item As Object
Dim i As Long
Dim ws As Worksheet
Set ws = Worksheets("JSON to Excel Example")
jsonText = ws.Cells(1, 1)
Set jsonObject = JsonConverter.ParseJson(jsonText)
i = 3
ws.Cells(2, 1) = "Color"
ws.Cells(2, 2) = "Hex Code"
For Each item In jsonObject
ws.Cells(i, 1) = item("color")
ws.Cells(i, 2) = item("value")
i = i + 1
Next
End Sub
I set a counter variable, i, that I can use to tell the loop which row to write the data to. Next, create my column headers for the data. Then, for each “item” in the jsonObject write that dictionaries values to the cell row I indicate and increment my counter variable. That’s it! The results of our import JSON to Excel code looks like this:
As you can see, we’ve turned that odd-looking string of text in cell A1 to an easy to understand table of data with just a few lines of code!
Understanding JSON to Excel (Advanced)
Now, I do want to point out that understanding the structure of the JSON you’ll be parsing is very important. Within the for each loop, the code can access the value of each object by referencing its key. This goes back to understanding the key/value pair relationship of JSON and the dictionary structure to which it is parsed. The reason I stress this so much is because, when you’re pulling back more complex looking JSON, you may need to have multiple levels of dictionary references to access the value you want. There may also be times when you have to nest for loops when you have an array of objects with arrays of objects nested inside each object.
For example, let’s look at an example where the JSON is structured as a single object with key “data” and the value is an array of data about employee’s and their organizations:
As you can see, we have an object with an array nested inside. Some objects in the array contains other nested objects as well. This multi-tiered data structure is very common when pulling data from a REST service and was very confusing to me when I first began trying to import JSON to Excel. Let’s examine what the code looks like for this scenario before we dive any deeper.
Sub JsonToExcelAdvancedExample()
Dim jsonText As String
Dim jsonObject As Object, item As Object
Dim i As Long
Dim ws As Worksheet
Set ws = Worksheets("JSON to Excel Advanced Example")
jsonText = ws.Cells(1, 1)
Set jsonObject = JsonConverter.ParseJson(jsonText)
i = 3
ws.Cells(2, 1) = "id"
ws.Cells(2, 2) = "Name"
ws.Cells(2, 3) = "Username"
ws.Cells(2, 4) = "Email"
ws.Cells(2, 5) = "Street Address"
ws.Cells(2, 6) = "Suite"
ws.Cells(2, 7) = "City"
ws.Cells(2, 8) = "Zipcode"
ws.Cells(2, 9) = "Phone"
ws.Cells(2, 10) = "Website"
ws.Cells(2, 11) = "Company"
For Each item In jsonObject("data")
ws.Cells(i, 1) = item("id")
ws.Cells(i, 2) = item("name")
ws.Cells(i, 3) = item("username")
ws.Cells(i, 4) = item("email")
ws.Cells(i, 5) = item("address")("street")
ws.Cells(i, 6) = item("address")("suite")
ws.Cells(i, 7) = item("address")("city")
ws.Cells(i, 8) = item("address")("zipcode")
ws.Cells(i, 9) = item("phone")
ws.Cells(i, 10) = item("website")
ws.Cells(i, 11) = item("company")("name")
i = i + 1
Next
End Sub
The code overall is the exact same, just referencing a different tab and added more fields to pull out. The change I want to point out is to the for loop itself. The beginning of the for loop now starts by referencing the overall object that contains the array. This is referred to as the “root” of the JSON and you’ll see it if you’re querying a REST API for JSON data. This moves the for each loop inside the “data” level of the JSON and will now loop through all objects in the highest-level array. Had I left this out, the loop would only occur once as the JSON is all contained within a single object at its top level.
The second piece I want to point out is the lines for address and company. These are both objects contained within the parent object. So, in order for me to reach to value I want, I have to reference both keys to get the specific value I need. Lastly, I want to call out that you do not need to write every value from the returned JSON to the sheet. I have skipped over several fields to get the end result I desired and it does not affect my code results, which coincidently look like this:
I hope this brief explanation of how to import JSON to Excel was useful for you! Please let me know if it was helpful or if you have any questions in the comments below. Also, if you want to download the file with the example JSON and VBA code, you can do so here if you’re a free subscriber to the site. It’s free, easy, and only takes a moment to sign up.
Cheers!
R
UPDATE 3 (Sep 24 ’17)
Check VBA-JSON-parser on GitHub for the latest version and examples. Import JSON.bas module into the VBA project for JSON processing.
UPDATE 2 (Oct 1 ’16)
However if you do want to parse JSON on 64-bit Office with ScriptControl
, then this answer may help you to get ScriptControl
to work on 64-bit.
UPDATE (Oct 26 ’15)
Note that a ScriptControl
-based approachs makes the system vulnerable in some cases, since they allows a direct access to the drives (and other stuff) for the malicious JS code via ActiveX’s. Let’s suppose you are parsing web server response JSON, like JsonString = "{a:(function(){(new ActiveXObject('Scripting.FileSystemObject')).CreateTextFile('C:\Test.txt')})()}"
. After evaluating it you’ll find new created file C:Test.txt
. So JSON parsing with ScriptControl
ActiveX is not a good idea.
Trying to avoid that, I’ve created JSON parser based on RegEx’s. Objects {}
are represented by dictionaries, that makes possible to use dictionary’s properties and methods: .Count
, .Exists()
, .Item()
, .Items
, .Keys
. Arrays []
are the conventional zero-based VB arrays, so UBound()
shows the number of elements. Here is the code with some usage examples:
Option Explicit
Sub JsonTest()
Dim strJsonString As String
Dim varJson As Variant
Dim strState As String
Dim varItem As Variant
' parse JSON string to object
' root element can be the object {} or the array []
strJsonString = "{""a"":[{}, 0, ""value"", [{""stuff"":""content""}]], b:null}"
ParseJson strJsonString, varJson, strState
' checking the structure step by step
Select Case False ' if any of the checks is False, the sequence is interrupted
Case IsObject(varJson) ' if root JSON element is object {},
Case varJson.Exists("a") ' having property a,
Case IsArray(varJson("a")) ' which is array,
Case UBound(varJson("a")) >= 3 ' having not less than 4 elements,
Case IsArray(varJson("a")(3)) ' where forth element is array,
Case UBound(varJson("a")(3)) = 0 ' having the only element,
Case IsObject(varJson("a")(3)(0)) ' which is object,
Case varJson("a")(3)(0).Exists("stuff") ' having property stuff,
Case Else
MsgBox "Check the structure step by step" & vbCrLf & varJson("a")(3)(0)("stuff") ' then show the value of the last one property.
End Select
' direct access to the property if sure of structure
MsgBox "Direct access to the property" & vbCrLf & varJson.Item("a")(3)(0).Item("stuff") ' content
' traversing each element in array
For Each varItem In varJson("a")
' show the structure of the element
MsgBox "The structure of the element:" & vbCrLf & BeautifyJson(varItem)
Next
' show the full structure starting from root element
MsgBox "The full structure starting from root element:" & vbCrLf & BeautifyJson(varJson)
End Sub
Sub BeautifyTest()
' put sourse JSON string to "desktopsource.json" file
' processed JSON will be saved to "desktopresult.json" file
Dim strDesktop As String
Dim strJsonString As String
Dim varJson As Variant
Dim strState As String
Dim strResult As String
Dim lngIndent As Long
strDesktop = CreateObject("WScript.Shell").SpecialFolders.Item("Desktop")
strJsonString = ReadTextFile(strDesktop & "source.json", -2)
ParseJson strJsonString, varJson, strState
If strState <> "Error" Then
strResult = BeautifyJson(varJson)
WriteTextFile strResult, strDesktop & "result.json", -1
End If
CreateObject("WScript.Shell").PopUp strState, 1, , 64
End Sub
Sub ParseJson(ByVal strContent As String, varJson As Variant, strState As String)
' strContent - source JSON string
' varJson - created object or array to be returned as result
' strState - Object|Array|Error depending on processing to be returned as state
Dim objTokens As Object
Dim objRegEx As Object
Dim bMatched As Boolean
Set objTokens = CreateObject("Scripting.Dictionary")
Set objRegEx = CreateObject("VBScript.RegExp")
With objRegEx
' specification http://www.json.org/
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = """(?:\""|[^""])*""(?=s*(?:,|:|]|}))"
Tokenize objTokens, objRegEx, strContent, bMatched, "str"
.Pattern = "(?:[+-])?(?:d+.d*|.d+|d+)e(?:[+-])?d+(?=s*(?:,|]|}))"
Tokenize objTokens, objRegEx, strContent, bMatched, "num"
.Pattern = "(?:[+-])?(?:d+.d*|.d+|d+)(?=s*(?:,|]|}))"
Tokenize objTokens, objRegEx, strContent, bMatched, "num"
.Pattern = "b(?:true|false|null)(?=s*(?:,|]|}))"
Tokenize objTokens, objRegEx, strContent, bMatched, "cst"
.Pattern = "b[A-Za-z_]w*(?=s*:)" ' unspecified name without quotes
Tokenize objTokens, objRegEx, strContent, bMatched, "nam"
.Pattern = "s"
strContent = .Replace(strContent, "")
.MultiLine = False
Do
bMatched = False
.Pattern = "<d+(?:str|nam)>:<d+(?:str|num|obj|arr|cst)>"
Tokenize objTokens, objRegEx, strContent, bMatched, "prp"
.Pattern = "{(?:<d+prp>(?:,<d+prp>)*)?}"
Tokenize objTokens, objRegEx, strContent, bMatched, "obj"
.Pattern = "[(?:<d+(?:str|num|obj|arr|cst)>(?:,<d+(?:str|num|obj|arr|cst)>)*)?]"
Tokenize objTokens, objRegEx, strContent, bMatched, "arr"
Loop While bMatched
.Pattern = "^<d+(?:obj|arr)>$" ' unspecified top level array
If Not (.Test(strContent) And objTokens.Exists(strContent)) Then
varJson = Null
strState = "Error"
Else
Retrieve objTokens, objRegEx, strContent, varJson
strState = IIf(IsObject(varJson), "Object", "Array")
End If
End With
End Sub
Sub Tokenize(objTokens, objRegEx, strContent, bMatched, strType)
Dim strKey As String
Dim strRes As String
Dim lngCopyIndex As Long
Dim objMatch As Object
strRes = ""
lngCopyIndex = 1
With objRegEx
For Each objMatch In .Execute(strContent)
strKey = "<" & objTokens.Count & strType & ">"
bMatched = True
With objMatch
objTokens(strKey) = .Value
strRes = strRes & Mid(strContent, lngCopyIndex, .FirstIndex - lngCopyIndex + 1) & strKey
lngCopyIndex = .FirstIndex + .Length + 1
End With
Next
strContent = strRes & Mid(strContent, lngCopyIndex, Len(strContent) - lngCopyIndex + 1)
End With
End Sub
Sub Retrieve(objTokens, objRegEx, strTokenKey, varTransfer)
Dim strContent As String
Dim strType As String
Dim objMatches As Object
Dim objMatch As Object
Dim strName As String
Dim varValue As Variant
Dim objArrayElts As Object
strType = Left(Right(strTokenKey, 4), 3)
strContent = objTokens(strTokenKey)
With objRegEx
.Global = True
Select Case strType
Case "obj"
.Pattern = "<d+w{3}>"
Set objMatches = .Execute(strContent)
Set varTransfer = CreateObject("Scripting.Dictionary")
For Each objMatch In objMatches
Retrieve objTokens, objRegEx, objMatch.Value, varTransfer
Next
Case "prp"
.Pattern = "<d+w{3}>"
Set objMatches = .Execute(strContent)
Retrieve objTokens, objRegEx, objMatches(0).Value, strName
Retrieve objTokens, objRegEx, objMatches(1).Value, varValue
If IsObject(varValue) Then
Set varTransfer(strName) = varValue
Else
varTransfer(strName) = varValue
End If
Case "arr"
.Pattern = "<d+w{3}>"
Set objMatches = .Execute(strContent)
Set objArrayElts = CreateObject("Scripting.Dictionary")
For Each objMatch In objMatches
Retrieve objTokens, objRegEx, objMatch.Value, varValue
If IsObject(varValue) Then
Set objArrayElts(objArrayElts.Count) = varValue
Else
objArrayElts(objArrayElts.Count) = varValue
End If
varTransfer = objArrayElts.Items
Next
Case "nam"
varTransfer = strContent
Case "str"
varTransfer = Mid(strContent, 2, Len(strContent) - 2)
varTransfer = Replace(varTransfer, """", """")
varTransfer = Replace(varTransfer, "\", "")
varTransfer = Replace(varTransfer, "/", "/")
varTransfer = Replace(varTransfer, "b", Chr(8))
varTransfer = Replace(varTransfer, "f", Chr(12))
varTransfer = Replace(varTransfer, "n", vbLf)
varTransfer = Replace(varTransfer, "r", vbCr)
varTransfer = Replace(varTransfer, "t", vbTab)
.Global = False
.Pattern = "\u[0-9a-fA-F]{4}"
Do While .Test(varTransfer)
varTransfer = .Replace(varTransfer, ChrW(("&H" & Right(.Execute(varTransfer)(0).Value, 4)) * 1))
Loop
Case "num"
varTransfer = Evaluate(strContent)
Case "cst"
Select Case LCase(strContent)
Case "true"
varTransfer = True
Case "false"
varTransfer = False
Case "null"
varTransfer = Null
End Select
End Select
End With
End Sub
Function BeautifyJson(varJson As Variant) As String
Dim strResult As String
Dim lngIndent As Long
BeautifyJson = ""
lngIndent = 0
BeautyTraverse BeautifyJson, lngIndent, varJson, vbTab, 1
End Function
Sub BeautyTraverse(strResult As String, lngIndent As Long, varElement As Variant, strIndent As String, lngStep As Long)
Dim arrKeys() As Variant
Dim lngIndex As Long
Dim strTemp As String
Select Case VarType(varElement)
Case vbObject
If varElement.Count = 0 Then
strResult = strResult & "{}"
Else
strResult = strResult & "{" & vbCrLf
lngIndent = lngIndent + lngStep
arrKeys = varElement.Keys
For lngIndex = 0 To UBound(arrKeys)
strResult = strResult & String(lngIndent, strIndent) & """" & arrKeys(lngIndex) & """" & ": "
BeautyTraverse strResult, lngIndent, varElement(arrKeys(lngIndex)), strIndent, lngStep
If Not (lngIndex = UBound(arrKeys)) Then strResult = strResult & ","
strResult = strResult & vbCrLf
Next
lngIndent = lngIndent - lngStep
strResult = strResult & String(lngIndent, strIndent) & "}"
End If
Case Is >= vbArray
If UBound(varElement) = -1 Then
strResult = strResult & "[]"
Else
strResult = strResult & "[" & vbCrLf
lngIndent = lngIndent + lngStep
For lngIndex = 0 To UBound(varElement)
strResult = strResult & String(lngIndent, strIndent)
BeautyTraverse strResult, lngIndent, varElement(lngIndex), strIndent, lngStep
If Not (lngIndex = UBound(varElement)) Then strResult = strResult & ","
strResult = strResult & vbCrLf
Next
lngIndent = lngIndent - lngStep
strResult = strResult & String(lngIndent, strIndent) & "]"
End If
Case vbInteger, vbLong, vbSingle, vbDouble
strResult = strResult & varElement
Case vbNull
strResult = strResult & "Null"
Case vbBoolean
strResult = strResult & IIf(varElement, "True", "False")
Case Else
strTemp = Replace(varElement, """", """")
strTemp = Replace(strTemp, "", "\")
strTemp = Replace(strTemp, "/", "/")
strTemp = Replace(strTemp, Chr(8), "b")
strTemp = Replace(strTemp, Chr(12), "f")
strTemp = Replace(strTemp, vbLf, "n")
strTemp = Replace(strTemp, vbCr, "r")
strTemp = Replace(strTemp, vbTab, "t")
strResult = strResult & """" & strTemp & """"
End Select
End Sub
Function ReadTextFile(strPath As String, lngFormat As Long) As String
' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII
With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, lngFormat)
ReadTextFile = ""
If Not .AtEndOfStream Then ReadTextFile = .ReadAll
.Close
End With
End Function
Sub WriteTextFile(strContent As String, strPath As String, lngFormat As Long)
With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True, lngFormat)
.Write (strContent)
.Close
End With
End Sub
One more opportunity of this JSON RegEx parser is that it works on 64-bit Office, where ScriptControl isn’t available.
INITIAL (May 27 ’15)
Here is one more method to parse JSON in VBA, based on ScriptControl
ActiveX, without external libraries:
Sub JsonTest()
Dim Dict, Temp, Text, Keys, Items
' Converting JSON string to appropriate nested dictionaries structure
' Dictionaries have numeric keys for JSON Arrays, and string keys for JSON Objects
' Returns Nothing in case of any JSON syntax issues
Set Dict = GetJsonDict("{a:[[{stuff:'result'}]], b:''}")
' You can use For Each ... Next and For ... Next loops through keys and items
Keys = Dict.Keys
Items = Dict.Items
' Referring directly to the necessary property if sure, without any checks
MsgBox Dict("a")(0)(0)("stuff")
' Auxiliary DrillDown() function
' Drilling down the structure, sequentially checking if each level exists
Select Case False
Case DrillDown(Dict, "a", Temp, "")
Case DrillDown(Temp, 0, Temp, "")
Case DrillDown(Temp, 0, Temp, "")
Case DrillDown(Temp, "stuff", "", Text)
Case Else
' Structure is consistent, requested value found
MsgBox Text
End Select
End Sub
Function GetJsonDict(JsonString As String)
With CreateObject("ScriptControl")
.Language = "JScript"
.ExecuteStatement "function gettype(sample) {return {}.toString.call(sample).slice(8, -1)}"
.ExecuteStatement "function evaljson(json, er) {try {var sample = eval('(' + json + ')'); var type = gettype(sample); if(type != 'Array' && type != 'Object') {return er;} else {return getdict(sample);}} catch(e) {return er;}}"
.ExecuteStatement "function getdict(sample) {var type = gettype(sample); if(type != 'Array' && type != 'Object') return sample; var dict = new ActiveXObject('Scripting.Dictionary'); if(type == 'Array') {for(var key = 0; key < sample.length; key++) {dict.add(key, getdict(sample[key]));}} else {for(var key in sample) {dict.add(key, getdict(sample[key]));}} return dict;}"
Set GetJsonDict = .Run("evaljson", JsonString, Nothing)
End With
End Function
Function DrillDown(Source, Prop, Target, Value)
Select Case False
Case TypeName(Source) = "Dictionary"
Case Source.exists(Prop)
Case Else
Select Case True
Case TypeName(Source(Prop)) = "Dictionary"
Set Target = Source(Prop)
Value = Empty
Case IsObject(Source(Prop))
Set Value = Source(Prop)
Set Target = Nothing
Case Else
Value = Source(Prop)
Set Target = Nothing
End Select
DrillDown = True
Exit Function
End Select
DrillDown = False
End Function
This builds on the terrific work of Daniel Ferry. Daniel wrote a lightweight and convenient script for parsing and manipulating JSON in VBA. I extended this a little bit to allow for the reading of JSON from the spreadsheet alone — no need for VBA from the user’s perspective.
'-------------------------------------------------------------------
' VBA JSON Parser
'-------------------------------------------------------------------
Option Explicit
Private p&, token, dic
Function ParseJSON(JSON$, Optional key$ = "obj") As Object
p = 1
token = Tokenize(JSON)
Set dic = CreateObject("Scripting.Dictionary")
If token(p) = "{" Then ParseObj key Else ParseArr key
Set ParseJSON = dic
End Function
Function ParseObj(key$)
Do: p = p + 1
Select Case token(p)
Case "]"
Case "[": ParseArr key
Case "{"
If token(p + 1) = "}" Then
p = p + 1
dic.Add key, "null"
Else
ParseObj key
End If
Case "}": key = ReducePath(key): Exit Do
Case ":": key = key & "." & token(p - 1)
Case ",": key = ReducePath(key)
Case Else: If token(p + 1) <> ":" Then dic.Add key, token(p)
End Select
Loop
End Function
Function ParseArr(key$)
Dim e&
Do: p = p + 1
Select Case token(p)
Case "}"
Case "{": ParseObj key & ArrayID(e)
Case "[": ParseArr key
Case "]": Exit Do
Case ":": key = key & ArrayID(e)
Case ",": e = e + 1
Case Else: dic.Add key & ArrayID(e), token(p)
End Select
Loop
End Function
'-------------------------------------------------------------------
' Support Functions
'-------------------------------------------------------------------
Function Tokenize(s$)
Const Pattern = """(([^""\]|\.)*)""|[+-]?(?:0|[1-9]d*)(?:.d*)?(?:[eE][+-]?d+)?|w+|[^s""']+?"
Tokenize = RExtract(s, Pattern, True)
End Function
Function RExtract(s$, Pattern, Optional bGroup1Bias As Boolean, Optional bGlobal As Boolean = True)
Dim c&, m, n, v
With CreateObject("vbscript.regexp")
.Global = bGlobal
.MultiLine = False
.IgnoreCase = True
.Pattern = Pattern
If .TEST(s) Then
Set m = .Execute(s)
ReDim v(1 To m.Count)
For Each n In m
c = c + 1
v(c) = n.Value
If bGroup1Bias Then If Len(n.submatches(0)) Or n.Value = """""" Then v(c) = n.submatches(0)
Next
End If
End With
RExtract = v
End Function
Function ArrayID$(e)
ArrayID = "(" & e & ")"
End Function
Function ReducePath$(key$)
If InStr(key, ".") Then ReducePath = Left(key, InStrRev(key, ".") - 1) Else ReducePath = key
End Function
Function ListPaths(dic)
Dim s$, v
For Each v In dic
s = s & v & " --> " & dic(v) & vbLf
Next
Debug.Print s
End Function
Function GetFilteredValues(dic, match)
Dim c&, i&, v, w
v = dic.keys
ReDim w(1 To dic.Count)
For i = 0 To UBound(v)
If v(i) Like match Then
c = c + 1
w(c) = dic(v(i))
End If
Next
ReDim Preserve w(1 To c)
GetFilteredValues = w
End Function
Function GetFilteredTable(dic, cols)
Dim c&, i&, j&, v, w, z
v = dic.keys
z = GetFilteredValues(dic, cols(0))
ReDim w(1 To UBound(z), 1 To UBound(cols) + 1)
For j = 1 To UBound(cols) + 1
z = GetFilteredValues(dic, cols(j - 1))
For i = 1 To UBound(z)
w(i, j) = z(i)
Next
Next
GetFilteredTable = w
End Function
Function OpenTextFile$(f)
With CreateObject("ADODB.Stream")
.Charset = "utf-8"
.Open
.LoadFromFile f
OpenTextFile = .ReadText
End With
End Function
'-------------------------------------------------------------------
' Functions to allow JSON parsing in spreadsheet
'-------------------------------------------------------------------
Function JSON(body$, path$)
Set dic = ParseJSON(body)
JSON = dic(path)
End Function
There are additional example on how to use this function to extract embedded objects or get a specific object in an array of objects. You can find these examples in the Example Spreadsheet.
One increasingly common task for Excel users is to retrieve data from the internet.
Simply, retrieving data from the web is the first challenge.
This sounds simple enough (and as you’ll soon see, it is).
But most people run into a roadblock with the second challenge: wrestling with data in JSON format.
Read on to discover the easiest and fastest method to get JSON data into Excel…
How to Import & Parse JSON Data with VBA
You’ve got several approaches to parsing data from the web which is in JSON format:
1) Roll Your Own
You could write your own VBA code to parse the JSON data.
This approach is only recommended if you have acute masochistic tendencies.
This path (of parsing JSON) has been travelled by others before you.
Leverage off their work 😉
2) Free / Open-Source Libraries
You can use free or open-source VBA libraries like VB-JSON or VBA-JSON.
While many of these libraries do work, some do have their own issues, may not be supported and often suffer from performance issues.
3) Third Party Libraries
You could buy a parsing library from a third-party provider.
But if you’re in a corporate environment, you may find it difficult to purchase non-standard software products.
In one recent project, it took 93 days from request to authorisation to installation to get my favourite VBA productivity suite, MZ-Tools installed.
Good thing I wasn’t in a hurry.
But if you are . . .
4) Standard COM components
The last approach, which we’ll cover here, is using standard COM components.
At the end of the day, Excel is just one big component.
What this means is that we can leverage other COM components and libraries quickly and (usually) easily.
Oh, and did I mention they’re free 🙂
What the heck is JSON?
Before we get cracking, let’s give a little attention to JSON.
You’re probably already familiar with XML.
Just like HTML uses tags to structure a webpage, XML uses tags to structure data. This data can be saved to a text file or transmitted over the internet.
JSON, which stands for JavaScript Object Notation, is another method of structuring data, although it doesn’t use tags.
Originally designed for JavaScript, JSON is now used widely for transmitting data between apps and across the web.
You can read all the gory details here.
Using a Web-service to Get Data
The web-service we’ll use to get data in JSON format for this example is Datamuse.
Datamuse is a word-finding query engine. It lets you retrieve a list of words based on a variety of criteria including meaning, spelling, sound and vocabulary.
For example, you could retrieve a list of words related to duck that start with the letter ‘b’, or adjectives that are often used to describe ocean.
The Magic of COM Libraries
You’ll be using two COM libraries to do all the heavy lifting for you.
The first is the Microsoft XML parser library which also includes the ability to query a website and retrieve data.
The second is the Microsoft Script Control
Retrieving the Web Data
The first thing you’ll need to do is set a reference to the Microsoft XML parser library in the Excel Visual Basic Editor (VBE):
You should find the Microsoft XML library in the list of References.
Its exact location may differ on your computer.
Once you’ve done that, in a new module, enter the following VBA function.
You can see it accepts a web URL as an input parameter and will return some text (as a string).
So, what’s it doing?
- The first couple of lines declare some constants to make the code easier to read and avoid including any “magic numbers”.
- You then declare a variable as the Microsoft XML parsing library and instantiate it.
- Next, you’ll open a connection to the web-service with the URL, request that the data come back in JSON format and send the request.
- Now we wait. We’ll just staying in holding pattern until the request (declared as a Microsoft XML object) tells us it’s finished.
- The function finally returns the JSON it retrieved from the web-service.
Testing Your Code
Let’s test it out.
Enter this routine above the GetResponse() function.
Remember, we’re using the Datamuse web-service to test retrieving JSON data. This query says:
Give me a list of adjectives used to describe the word ‘ocean’.
Let’s test it out.
Enter the following test routine above the GetResponse() function.
Remember, we’re using the Datamuse web-service to test retrieving JSON data.
This query says: Give me a list of adjectives used to describe the word ‘ocean’.
If you pasted this URL into your web browser, it’d look something like this:
Making Sense of JSON
Okay, we’re half-way there.
You’ve got a clean and simple way to retrieve data from a web-service.
Now you just need an efficient way to parse the JSON and load it into Excel.
Enter the Script Control . . .
Now set a reference to the Microsoft Script Control in the Excel VBE:
The script control has several other purposes, including the interpretation and execution of scripts in a host application.
But for your purposes, you just need it to parse JSON.
Enter the following function in your module:
Let’s have a look at what it’s doing . . .
- First you declare your ScriptControl variable and set the Language property to «JScript», Microsoft’s flavour of JavaScript.
- Next you declare another Object variable. You then use ScriptControl’s Eval to parse the JSON into a structure you can interrogate.
- Here we want to populate an array with the Word collection in the items object, but first we use a helper function called GetItemCount to count the number of elements in the items object to size the array correctly.
Yes, you could use ReDim Preserve to keep resizing the array but it’s slow and inefficient.
I’ve listed the GetItemCount helper function below. - Next you loop through each element in the items object and assign each successive value to your array.
- Finally, just for good measure, return the number of elements found.
Now you have a simple routine to parse your JSON data, enter the specific data you want into an array and tell you how many items it found.
What’s in the JSON?
Before we look at the code in more detail, let’s take a quick peek at what’s inside that JSON object we retrieved from the web-service.
Remember, what was returned is a JSON object, not a VBA array.
However, you can peer inside the object as well as loop through it to pluck out data you’re interested in.
When the MS Script Code does is convert the blog of JSON text you saw in the browser screenshot above to something structured.
If you looked at it in the VBE Locals window, it would look like this:
- Notice that the structure “looks” like a regular array with a hierarchy.
- But remember, it’s really a JavaScript object.
Okay, to be pedantic, it’s a JScript – Microsoft’s ‘version’ of JavaScript – object.
Now, back to the code . . .
- The first few lines declare a variable, script, as being a shiny new MS Script Control object and tells it you want to use “JScript”. The other alternative is “VBScript”.
- You then define an object variable called items (original, I know) to hold the data structure. This is then populated by asking the Script Control to evaluate the JSON.
- As this isn’t a true array you can’t use UBound() to determine how many items it has. Instead, you can use a helper function to loop through the Items object and count them.
You’ll then use this number to resize the output array, ready to be populated. This also helps avoid using ReDim Preserve which can be a tad on the slow side. - You then loop through each top-level element in the object, retrieving the value for “word” and passing it to the VBA array.
Just a minute . . .
How did you know the element you wanted was called “word”?
It’s right there in the JSON string that you retrieved earlier.
Remember, JSON is composed of a couple of simple data structures, the main one being an unordered set of name/value pairs (essentially a dictionary).
Now that you’ve looped through the object and populated the array that was passed by reference, the function returns a count of how many items were found.
Now let’s see it in action.
Update the Test_GetResponse routine so it looks like this:
Let’s pull this code apart. . .
- First you declare some variables you need for this process. One to hold the JSON data returned from the webservice (json), an array to hold the values parsed from the JSON object (words) and a range object (Target) so you can paste the results to a worksheet.
- Next you call the GetResponse function, passing the URL and have the resulting JSON assigned to the json variable.
- Finally, you call the ParseJson function, passing the JSON object and the array to be populated.
If this returns a value greater than zero, you swing into action and define where you want the list of words entered and assign the array to that range.
The eagle-eyed among you probably noticed that you’re also flipping the array using the worksheet Transpose function before assigning it to the Target range.
Meanwhile, you should now have a list of words in worksheet one.
You could have used this to populate a listbox, create a report, anything you’d normally do with extract data.
Over to You
Admittedly, the JSON data structure used in this example was not very complicated. However, the same principles apply to more complex data structures.
Take your time to example the returned data in the Locals window and you’ll be in a better position to parse even the most complicated JSON data.
What challenges have you faced importing and processing JSON data into Excel. How did you solve them?
Let me know in the comments below.
If you’re still wrestling with some JSON data let me know about that too.
What is JSON?
JSON (JavaScript Object Notation) is a file format that is used for storing and exchanging data in the network. It is used to send data from server to client and client to server. JSON is easy for machines to analyze and generate. The extension of JSON is .json.
JSON records are, in reality, straightforward to peruse on the off chance that you know the construction of a Javascript document. You can utilize a proofreader with explicit help for JSON to get the disentangling. For instance, if there is an extremely pleasant JavaScript addon for Notepad++ called JSTool, which incorporates a JSON watcher, it shows the decoded esteem strings so you can duplicate the string to another content record.
Check Out upGrad’s Java Bootcamp
A commonplace JSON record is a word reference where every section has a string key, and each worth is a string, a number, or exacting like valid, bogus or invalid. The qualities are strings that are encoded by the shows of Javascript, which is practically equivalent to Java or C, implying that every typical character is composed with no guarantees and all extraordinary characters are encoded with an oblique punctuation line.
This is either followed by the scorch that is implied or by a hex number that addresses the ASCII or Unicode estimation of the singe. For example, on the off chance, you have French or Spanish scorches or dialects that are totally outside Latin letters in order like Russian or Chinese.
JSON is JavaScript object documentation or a progression of JavaScript objects with recorded credits. These credits are what is recorded within each set of wavy sections and give data about the object. JSON records may contain numerous objects in each document. They might be the consequence of an information dump from a program that utilizes JSON documentation or methods by which a client can enter data into a data set. MongoDB, for instance, locally utilizes JSON to store information.
A JSON document is a record that stores straightforward information constructions and objects in JavaScript Object Notation (JSON) design, which is a standard information trade design. It is principally utilized for sending information between a web application and a server. JSON documents are lightweight, text-based, comprehensible, and can be altered by using a content manager.
Also, check out our free courses to get an edge over the competition.
Open the record in Notepad, save it as a .TXT document, and then import it into Excel.
The Structure of JSON is
Filename = [{key: value}]
Example
JSONExample ={ “namelist” : [ {name:”mark”, age:18, location:” Mars”},
{name:Steve Rogers, age:118, location:”Moon”},
{name:”Tony”, age:48, location:”DC”},
{name:”Peter Parker”, age:18, location:”Queens”},
{name:”Thor”, age:18, location:”Ascord”} ],
“id”:1
}
Applications of JSON
- JSON files are primarily used while writing programming code in Javascript-based programs like node, react, etc.
- The JSON file is used for transmitting data in a structured way while transferring via a network.
- Most of the time JSON is used to transfer data between server and client applications.
- In the modern era, JSON overtakes XML in the maximum amount.
- It can adapt to all programming languages.
upGrad’s Exclusive Software and Tech Webinar for you –
Check Out upGrad’s Advanced Certification in Cyber Security
SAAS Business – What is So Different?
What is Excel?
Excel is the name of a software program created by Microsoft and is known as Microsoft Excel. It uses a spreadsheet to store the data and numbers with the functions and formulas. The extension of the excel file is .xlsx. It is widely used in the business sector.
There are important things about excel that you can learn at upGrad. Excel is perhaps the most remarkable measurement software. It permits you to store the information in even arrangement, i.e., in lines and segment design. It additionally allows you to collaborate with your data differently.
You can sort and channel the information utilizing probably the most powerful equations. The turntables one is the best component of Excel. You can use turntables to make another understanding by controlling the information. Excel has different highlights that can assist you with insights. There are different approaches to bringing in and sending out the information. You can likewise incorporate the data into the work process.
Like no different insight software, Excel permits you to make the custom capacity utilizing its programming capacities. The basic role of Excel is to make records of information and to control the information according to the users’ requests. As referenced before, Excel permits you to utilize the outside information base to dissect and make reports, among other things.
Currently, Excel offers the best graphical UI alongside the utilization of design instruments and representation strategies.
Source
Applications of Excel
1. Easy to Utilize Interface
MS Excel has an easy-to-use interface that empowers the client to utilize the software effectively without battling an excessive amount to keep up any records.
2. Graphical Representation
MS Excel empowers the client to graphically address the information entered. This component of excel helps break down the numbers easily when we manage an enormous amount of data. When handling a vast amount of data (extending to 1000 lines), graphical representation helps us assess the information quickly.
3. Highly Supportive in Breaking Down Deals
Excel helps organizations break down deals and buys and appropriately assists them with examining their purchasing/selling choices.
Our learners also read: Excel online course free!
4. Financial Modeling
Quite possibly, the main benefit of Excel is that it simplifies the financial analysis of the organization. It helps with maintaining the financial records of the organization in an easily accessible format. Excel is often used to calculate or determine EBITDA, WACC, net sales, and net profits, among other things.
5. Accessibility of a Broad Scope of Functions
Excel has enormous scope for doing equations which assists us with computing the entirety, item, difference, and any arrangements of numbers. The user can call upon various functions to simplify the vast data at hand to offer an accurate deduction.
6. Future Development of Any Business
Excel is a valuable instrument to break down different sorts of information. Organizations use Excel to get an accurate picture of their business development and get a realistic forecast of the coming years’ achievable growth. Excel helps organizations accomplishing a particular development rate by examining the progress of the business.
How to Open JSON file in EXCEL?
The methods of opening JSON files in Excel vary in different versions of Microsoft Excel. You can apply the following methods to open a JSON file depending on the versions of Excel you have:
Office 2007
In the 2007 version of Office, excel doesn’t have the option to open a JSON file in EXCEL.
Office 2010
- After opening Excel, on the Power Query tab, click From Other Sources > Blank Query.
- In the Query Editor, click Advanced Editor.
- Input your query string as follows, replacing “C:UsersNameDesktopexample.json” with the path to your JSON file.
let
Source = Json.Document(File.Contents(“C:UsersNameDesktopexample.json”)),
#”Converted to Table” = Record.ToTable(Source)
in
# “Converted to Table”
Office 2013
In the 2013 version of Office, excel doesn’t have the option to open a JSON file in EXCEL.
- After opening Excel, on the Power Query tab, click From Other Sources > Blank Query.
- In the Query Editor, click Advanced Editor.
- Enter your query string as follows, replacing “C:Usersexample.json” with the path to your JSON file.
let
Source = Json.Document(File.Contents(“C:Usersexample.json”)),
#”Converted to Table” = Record.ToTable(Source)
in
# “Converted to Table”
In Newer Version of Excel
- Select Data > Get Data > From File > From JSON. The Import Data dialog box appears.
- Search the JSON file, and then select Open.
Opening JSON From the Internet in Excel
The above methods are used for opening files stored in local storage. If you want to import JSON data from the internet or from an API call, you can do the following:
- Go to Data Tab -> Transform data -> Click From Web. It will open a window or modal.
Source
- Then, open the power query.
Source
- Next, create a table for the JSON.
Source
- By following the steps, you can open the JSON file in EXCEL.
Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
Conclusion
JSON (JavaScript Object Notation) is a file format that is used in storing data and exchanging data in the network. EXCEL uses a spreadsheet to store the data and numbers with functions and formulas. The steps for opening a JSON file in Excel differ in several versions of EXCEL. In the newer version, you can open a JSON file by selecting “Data” > Get Data > From File > From JSON. The Import Data dialogue box appears. Search the JSON file, and then select “Open.” You can check out Free online Excel courses offered by upGrad on Excel
If you’re interested to learn more about full-stack development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
What is a json format?
JSON is a format of storing and exchanging data. It stands for JavaScript Object Notation. JSON is a lightweight data-interchange format. It is used primarily to transmit data between a server and web application, as an alternative to XML. It is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. This data interchange format is simple, small, and language independent JSON was derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. It has become quite popular for transporting data between the server and web application. It is considered as a lightweight or language independent alternative to XML and is used in numerous AJAX applications.
How to view json files?
JSON file is actually a text file, you can view it line by line with simple text editor like notepad, notepad++ or emacs and so on. If you want to view it in python, it’ll be better to install jq, in that case, you can view it in python like: jq ‘.result’. There are many available JSON libraries for almost every language. If you are interested in Java, you may want to check out GSON. If you want to use for Python, you can also use simplejson.
What are the applications of JSON files?
JSON files are files that store data in JavaScript Object Notation. This is a lightweight format for storing data which is easy for humans to read and write. One of the best features of JSON files is that they are readable by both machines and humans (unlike XML) and more importantly, they work well with JavaScript. JSON files are commonly used for data-driven web applications. In fact, you can even use them to make a web page dynamic. JavaScript has the ability to parse these files and that’s why JSON is one of the best ways to return data to a web page. You can store data in a well-organized and easily-readable format.
Want to share this article?
Land on Your Dream Job
In this article, we will learn how to perform the How can you extract a nested JSON value in VBA Excel using VBA-Json Parse Nested Json package.
In this article, I will explain to you a step-by-step for performing Parsing complex JSON Data using VBA JSON in Excel VBA.
You can download the source code at the bottom of the post
Recently I’m working on a Excel VBA project in which I need to call the rest and parse the json response. I google this question and but can’t find any good article which covers this topic with complex json response. so that I decided to write an article on Parsing Nested Arrays using VBA and JSON.
VBA Excel doesn’t have a built JSON parser for us , that’s why are going to use VBA-tools JSON parser package to parse the JSON that we receive after making a rest api calll.
Go to the GitHub link : https://github.com/VBA-tools/VBA-JSON
and download code.
And open your Excel sheet and go to the developer tool and visual basic and then import the JSON converter that we download from Github.
Go to File and click on Import file and naviate to the folder that and select the JsonConverter.bas And click on Open.
So now go back to the visual basic IDE and click on Tools and select references and Microsoft scripting runtime references in our project.
So let write code for parsing json.
Simple Json Parsing Example
Private Sub ParseSimpleJson() Dim JsonObject As Object Dim strResponse As String strResponse = "{""name"": ""johny"", ""address"": { ""country"": ""USA"",""city"": ""New York City"" } }" Set JsonObject = JsonConverter.ParseJson(strResponse) MsgBox ("User name :" & JsonObject("name") & " User Country: " & JsonObject("address")("country")) End Sub
1. Parsing Nested Arrays using VBA and JSON
Sample json
{ "apidata": { "success": true, "data": { "music_events": { "Magic": { "users": ["Tayler", "Ally"], "event_status": "Pending", "event_sites": { "ticketbet": { "odds": { "h2h": ["1.86", "1.99"] }, "last_update": 1488956952 }, "stream411": { "odds": { "h2h": ["1.70", "2.10"] }, "last_update": 1488957101 }, "darkmusic": { "odds": { "h2h": ["1.83", "1.98"] }, "last_update": 1488957104 }, "lastride": { "odds": { "h2h": ["1.83", "2.00"] }, "last_update": 1488957115 } } } } } } }
Our goal is to get the music_events, users details into table and the event_sites data into a separate table.
VBA Code for parsing above json
Private Sub ParseNestedJson() Dim JsonObject As Object Dim strResponse As String Dim music_events, k, users, v, event_sites strResponse = Sheet1.Range("A1").Value Set JsonObject = JsonConverter.ParseJson(strResponse) Set music_events = JsonObject("apidata")("data")("music_events") For Each k In music_events Debug.Print "event", k Set users = music_events(k)("users") For Each v In users Debug.Print , "participant", v Next v Set event_sites = music_events(k)("event_sites") For Each v In event_sites Debug.Print , "site", v Next v Next 'MsgBox ("User name :" & JsonObject("name") & " User Country: " & JsonObject("address")("country")) End Sub
2. Parsing complex JSON Data using VBA JSON
Sample Json
{ "UniqueId": "{344DSD-343-34D-343-23SDSDSD}", "from": "2021-01-16", "to": "2021-01-22", "data": [ { "date": "2021-01-16", "person": "{34343DS-343-3434-343-SFDSS343}", "personName": "Rohit Smith", "company": "{SDSD344-343-343-343-3FDFDFD}", "companyName": "Appsloveworld pvt ltd", "minutes": "400", "task": [ { "name": "Training", "code": "TRN", "minutes": "120" }, { "name": "Human Resources", "code": "HR", "minutes": "150" }, { "name": "Yoga", "code": "YG", "minutes": "15" }, { "name": "Lunch", "code": "", "minutes": "30" } ] } ] }
There may be any number of ‘data’ records, as well as any number of ‘tasks’ within each “data” including zero.
we want a row in the spreadsheet for each activity, with the task name and other data outputted next to that day’s task.
VBA Code for that
Sub NestedJsonExample() Dim ts, act Dim Json As Object, c As Range, strResponse As String 'reading json from a worksheet cell... strResponse = Sheet1.Range("B1").Value Set Json = JsonConverter.ParseJson(strResponse) Set c = ActiveSheet.Range("C5") 'loop over timesheets For Each ts In Json("data") 'loop over timesheet activities For Each act In ts("task") c.Resize(1, 11).Value = Array(Json("UniqueId"), Json("from"), Json("to"), _ ts("date"), ts("personName"), ts("companyName"), _ ts("minutes"), act("name"), act("code"), _ act("minutes")) Set c = c.Offset(1, 0) Next act Next ts End Sub
Download Source Code
Some information Regarding VBA
Excel VBA is the programming language of Microsoft Excel like for Microsoft Office projects like Word and PowerPoint.
VBA is the truncation for Visual Basic for Applications. It is an occasion driven programming language from Microsoft. Which is currently fundamentally utilized with Microsoft Office applications like MS-Excel, MS-Word and MS-Access. It helps in the making of tweaked applications and their answers, which improve the abilities of those applications. The benefit of this component is that we don’t have to introduce Visual Basic on our PC yet introducing Office assists us with accomplishing our target.
We can utilize VBA in all renditions of Office from MS Office 97 to MS Office 2013. You can likewise explore different avenues regarding other present day forms of Office that are accessible with them. Dominate VBA is the most famous of all VBA and the benefit of utilizing VBA is that we can assemble an amazing asset utilizing Linear Programming.
Visual Basic is a programming language that accompanies a coordinated advancement climate. Planned by Microsoft, the Visual Basic download makes coding a basic and pleasant experience. Reasonable for all clients, including fledglings and specialists, this language is object-driven and gives you admittance to sentence structure developments and an information base of components. You can fabricate a scope of Windows applications and front-end frameworks.
What is Windows Visual Basic?
Visual Basic is an article driven improvement climate and PC programming language made by Microsoft. The framework gives a graphical UI that permits them to alter the code by relocating components, permitting clients to change the appearance and conduct of the application. The article arranged language depends on BASIC and is considered appropriate for amateurs to code.
Microsoft expected to improve on the language and backing quicker coding. That is the reason it is known as RAD or Rapid Application Development System. With its assistance, coders can model applications prior to thinking of them in a more effective however troublesome dialect. What’s more, Virtual Basic likewise gives punctuation that is more clear and data set associated.
The post Simple way to Parse JSON with Excel VBA appeared first on Software Development | Programming Tutorials.
Read More Articles
- Excel VBA macro using iTunes search API — fastest way to query & parse JSON results
- How to parse JSON with VBA without external libraries?
- JSON VBA Parse to Excel
- Parse JSON with VBA (Access 2010)
- Quickest Way to open an excel file with VBA
- Excel VBA throws overflow error with a simple division
- Simple way to refresh power pivot from VBA in Excel 2010?
- Excel VBA Arrays: Is there a simple way to delete a dataset by the index?
- Parse simple two dimensional JSON array in VBA without external libraries
- MS Excel 2003 — Simple unselect question in Excel VBA when dealing with shapes
- Best way to distribute Excel spreadsheet with VBA
- Simple recursive function in VBA with Excel not returning expected result
- Fastest way of Parsing Json to Excel using VBA
- Is there a simple way to parse comma separated Key:Value pairs in Excel, Power Query or VBA if the values contain unescaped commas?
- parse a string with VBA on EXCEL
- Parse local webpages with Selenium in VBA Excel fails
- How can I parse json in Excel vba without using microsoft scripting runtime?
- How to fix and extract google CSE JSON API results with excel vba
- Using VBA in Excel to retrieve a Json from an API (problem with the API password)
- VBA Json Parse response with JsonConverter
- Parsing Google Books JSON to obtain book info by entering ISBN in EXCEL with VBA
- Review my simple VBA script with built-in Excel function
- Excel VBA out of memory error with simple line of code
- get data from a JSON string with VBA Excel
- Best way to get column totals on multiple columns with varying row sizes using MS Excel VBA
- Faster Way to copy files from one folder to another with Excel VBA
- VBA code in excel operates inconsistently with very simple code
- VBA Excel — Problems with a simple macro to auto-fill cells for a budgeting spreadsheet I’m attempting to make
- excel to json with vba
- With Excel VBA is there a way to copy an entire worksheet’s NumberFormat to a new worksheet?
- Stop IE from loading further when required data is there
- VBA Excel : Populate TextBox with specific string of text when Option Button is selected
- Object Defined Error in Data from Internet Pull
- Range.Find does not find exactly what I’m searching for
- How to disable arrow keys in ListView control in VBA with Excel
- Run-time error ‘1004’: The PivotTable field name is not valid
- Weird activecell.offset output
- Move files from multiple folders to a single folder
- moving range of values from one sheet to another
- VBS code not working in HTA (WScript declaration)
- VBA Form Is (automatically) Tied/Bound To ActiveWorksheet When calling Form.Show vbmodeless
- Make the second combo-box item selected based on item selection of first combo box
- How to check the same values between 2 table (MSAccess)
- How do you run a Word mail merge macro from Excel?
- Stop procedure if condition is not met using a global variable in VBA
- Setting default values in Data Validation drop-down list
- Pivot table expand and substract or Show/Hide Details using Excel VBA
- Using one or two dimensional arrays in Excel VBA
- Summing up a column based on multiple Criterion
- Calculate Time of each row excel