R Input Json File
# R JSON File
JSON: **J**ava**S**cript **O**bject **N**otation.
JSON is a syntax for storing and exchanging text information.
JSON is similar to XML, but smaller, faster, and easier to parse.
If you are not familiar with JSON, you can first check: (#)
Reading and writing JSON files in R requires installing an extension package. We can enter the following command in the R console to install:
install.packages("rjson", repos = "https://mirrors.ustc.edu.cn/CRAN/")
Check if the installation was successful:
> any(grepl("rjson",installed.packages())) TRUE
Create a sites.json file, the json file is in the same directory as the test script. The code is as follows:
## Example
{"id":["1","2","3"], "name":["Google","Tutorial","Taobao"], "url":["www.google.com","www.","www.taobao.com"], "likes":[111,222,333]}
Next, we can use the rjson package to load the data from the json file.
View data, use for a specific row, and [] for specified rows and columns:
## Example
# Load rjson package
library("rjson")
# Get json data
result <- fromJSON(file="sites.json")
# Print result
print(result)
print("===============")
# Print result of column 1
print(result)
print("===============")
# Print result of row 2, column 2
print(result[][])
The output of the above code is:
$id "1" "2" "3" $name "Google" "Tutorial" "Taobao"
YouTip