Hands-On Exploratory Data Analysis with R
上QQ阅读APP看书,第一时间看更新

Loading the jsonlite package

The following command is executed to include the jsonlite package in the R workspace:

library(jsonlite)

Data processing or parsing is done through a process called simplification. This is where JSON arrays are converted from a list into a more specific R class. Basically, with a simplification process, the arrays are broken down into values that can be easily understood. There are three different options that can be passed as arguments to the fromJSON function:

  • simplifyVector: This option is used to convert arrays into a vector format
  • simplifyDataFrame: This option is used to convert arrays into a data frame format with representation of rows and columns
  • simplifyMatrix: This is similar to the data frame format, the only difference being that data should be included as a numeric representation

Let's look at an example of a data frame:

json_data <-
'[
  {"Name" : "John", "Age" : 42, "City" : "New York"}, 
  {"Name" : "Jane", "Age" : 41, "City" : "Paris"},
  {},
  {"Name" : "Bob", "City" : "London"}
]'
df <- fromJSON(json_data)
df

Here, json_data key values are converted into a data frame.