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
The syntax for JSON comes from JavaScript and can be summarized as follows:
{“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.
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.
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.
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.
| 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 |
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.
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.
The JSON column has varied lengths and objects and strings. How would you go about parsing this custom JSON data so that it is all available in one table?
Post your solutions in the comments section below. If you get stuck, post a comment letting me know where you are stuck and what problems you encounter.
The video below shows how you can parse simple JSON data using Excel Power Query and how you can parse custom JSON data as detailed in the activity.
Practical Beginner Excel Now Includes Learn and Earn Activities.
Click to find out more now and start earning while you are learning Excel and Power BI
IF YOU CARE- YOU WILL SHARE - YOU WILL FIND THE SHARE BUTTONS BELOW THE COMMENTS SECTION