jsondiff
Overview
jsondiff finds and renders difference in values between JSONs. Diffing R objects (like lists and dataframes) is also supported by automatic conversion to JSON. It is implemented as an R interface to jsondiffpatch, powered by htmlwidgets framework.
Installation
devtools::install_github("bergant/jsondiff")
Usage
Compare two lists:
x <- list(
name = "Pluto", orbital_speed_kms = 4.7, category = "planet",
composition = c("methane", "nitrogen")
)
y <- list(
name = "Pluto", category = "dwarf planet", orbital_speed_kms = 4.7,
composition = c("nitrogen", "methane", "carbon monoxide")
)
library(jsondiff)
jsondiff(x, y)
Show also unchanged data:
jsondiff(x, y, hide_unchanged = FALSE)
Character vectors of size 1 are evaluated as JSONs:
library(jsonlite)
json_x <- toJSON(x)
json_y <- toJSON(y)
jsondiff(json_x, json_y)
Data frames (example is from daff package, which is probably a better choice to handle difference between data frames than jsondiff):
x <- y <- iris[1:3,]
x <- head(x,2) # remove a row
x[1,1] <- 10 # change a value
x$hello <- "world" # add a column
x$Species <- NULL # remove a column
jsondiff(y, x)
Note that there are several options when converting R objects to JSON. Instead
of using toJSON
explicitly, one can use json_options
. For example,
converting from data frame to JSON as "column first":
jsondiff(y, x, json_opt = json_options(dataframe = "columns", factor = "string"))
Related projects
Made possible by:
- jsondiffpatch - Diffing and patching JSONs (JavaScript library)
- htmlwidgets - Framework for easily creating R bindings to JavaScript libraries
- jsonlite - A Robust, High Performance JSON Parser and Generator for R
See also:
- Daff - Diff, patch and merge for data.frames
- listviewer - R htmlwidget to view lists
- diffobj - Compare R Objects with a Diff
- tools::Rdiff: Difference R Output Files
License
MIT