Displaying from a JSON file in R and R Markdown

Starting with R

Using the jsonlite package we can display data from JSON files in R. Make sure you install jsonlite first, if it isn't already installed:
 
Using fromJSON we can read the JSON file and assign it to a variable. You can find the attached sample file. Make sure it is in your working directory. You can check your working directory with getwd() and change it with setwd().
 
We can then show the data type of the file, print it's contents, ect.
 
class() to show the data type:
 
print() to show the files contents:

Using R Markdown

We can use plotly with jsonlite to knit the data in the json file with R Markddown. First install plotly, if you haven't already:
 
 
Make sure to use the jsonlite and plotly libraries, then we can use fromJSON from jsonlite and plotly_json from plotly to assign the json data to a variable:
 
 
Then we can call the variable with `r [variable]` and knit to HTML:
 

Converting JSON to a data frame

After assigning the reading and assigning the data in our JSON file to a variable we can conver it to a data fram with as.data.frame():
 
With our data frame we can use the dplyr library to return specific columns by name:
 
We can also return columns by location (remember the first column is in the 0 position):
 

Conditions to Select Columns and Data Types

We can use conditionals to filter columns by data type. If we assume that the Salary column is numeric, we can return it with json_data %>% select_if(is.numeric):
 
There are no columns with the numeric value, but we know that some of the columns should in fact be numeric. We can check the type of each column in our data frame with str():
 
We can use transform() to change the data type of each column:
 
We can ignore the warning messages by using suppressWarnings(). We can see that all of the columns now have the correct data types but that some of the start date values are NA. This is because all of the values are not in the same format. Some of them are m/dd/yyyy and some are mm/dd/yyyyy. We can fix this by adding the format arugument to our as.Date call. Lets use as.Date(StartDate, format = "%m/%d/%Y") to ensure that all of our dates are converted properly whether they are formated as 01/01/2021 or 1/1/2021:
Topic attachments
I Attachment Action Size Date Who Comment
input.jsonjson input.json manage 410 bytes 02 Dec 2020 - 02:54 CashBarnes The sample file used in all of the examples. Make sure it is in your working directory.
Topic revision: r4 - 03 Dec 2020, CashBarnes
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co