How to Parse Custom JSON Data using Excel

in #excel6 years ago (edited)


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

How to Parse Custom JSON Data using Excel

{“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:

excel JSON

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

How to Parse Custom JSON Data using Excel

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.

How to Parse Custom JSON Data using Excel

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

How to Parse Custom JSON Data using Excel

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.

How to Parse Custom JSON Data using Excel

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.

How to Parse Custom JSON Data using Excel

To parse the json column, first select the column, then on the Transform Ribbon select Parse and select JSON

How to Parse Custom JSON Data using Excel

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.

How to Parse Custom JSON Data using Excel

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.

How to Parse Custom JSON Data using Excel

As we do not need the name of the array, we can use the filter to remove all the follow rows

How to Parse Custom JSON Data using Excel

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

How to Parse Custom JSON Data using Excel

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

How to Parse Custom JSON Data using Excel

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.

How to Parse Custom JSON Data using Excel

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.

Learn and Earn Activity


Look at this small table of data.  You can download the file with this link to carry out this Learn and Earn Activity.  This is more complex custom JSON data that can be parsed in Excel with Power query.


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?

Give it a try.  If you have a STEEM account you can earn while you learn. See below for more details.

It does require logical thinking and little more Power Query knowledge than what we have covered here but I know you can get it.  Also, there is more than one way to come up with the solution.

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.

Become a Power Pivot Hero



Power Pivot online training course

WE REWARD YOU for Learning EXCEL

Practical Beginner Excel Now Includes Learn and Earn Activities.

FREE beginner excel training

Time to Brush Up on Power Pivot 

Power Pivot online training course
learn and earn steem activity
Now there is value in Learning with The Excel Club and our Learn and Earn STEEM activities.
We are the first Excel, PowerBI and DAX blog in the world where you can Earn while you Learn.

Click to find out more now and start earning while you are learning Excel and Power BI


actifit

BEST VALUE


ACCESS ALL AREAS - UNLIMITED LEARNING




Sign up for my newsletter – Don’t worry, I won’t spam. Just useful Excel and Power BI tips and tricks to your inbox with Earn and Learn activities.


SIGN UP NOW


New to Excel? Check out our Ultimate beginner Excel Guide here.

New to Excel Power Tools? Check out these amazing Power Pivot, Power Query and Power view demonstrations


New to DAX for Power Pivot and Power BI? Let us help you get started


Sign up for my newsletter – Don’t worry, I won’t spam. Just useful Excel and Power BI tips and tricks to your inbox with Earn and Learn activities.

SIGN UP NOW

IF YOU CARE- YOU WILL SHARE - YOU WILL FIND THE SHARE BUTTONS BELOW THE COMMENTS SECTION


Cross posted from my blog with SteemPress : http://theexcelclub.com/how-to-parse-custom-json-data-using-excel/

Sort:  

Hi Paula,
I was following the example and hit a snag. When I try to parse, I receive an error message. I tried to research this on the internet to no avail. My screen looks exactly like the one above, but when I parse, every row in the json_metadata column says Error. What might I be doing wrong?

I have just come back to try this now and I am also getting the same error

Hi @coop78 and @dernan I have updated the post to contain download links to files for the activities. When you copy and paste the tables into excel, the json field is not recognized as a json field. So please work of the downloads.

Can someone share the download file for this project? importings JSON INTO Excel. The link doesn't work for the project.
- michael hughes

This comment was made by a guest account using @steempress. Rewards will be sent to the user once they have a Steem account

This is quite advanced stuff paula, I got your email thanks. Its a little advanced for me, but I have shared this post with my boss as I think he would be rather intersted

thanks for the share @dernan, and yep it is advanced, but you should try it, its not that difficult. And next week I will have the video solution to the activity

Not that anyone should quote me on this one, but it seems that jsons are pretty much standard when it comes to API development.

Standard way data ca be moved around the web via API calls but all different in the data they contain

Posted using Partiko Android

This method certainly beats using SQL for simplicity!

Nice promotion of @actifit from your Wordpress blog too :)

glad you find it usefull @abh12345. I put @actifit in so people can see there are other ways to also earn steem, get people familiar with some of the uses before I do the hard launch.

This is simply awsome: I'll try it, thank you!

Resteemed and (sorry, very little) upvoted!

There is so much people can do with Excel, now with the power tool, working with data of different types is so much easier that it was before.

Congratulations @theexcelclub! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 600 upvotes. Your next target is to reach 700 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!