How to convert a list to JSON in R?

R ProgrammingServer Side ProgrammingProgramming

To convert a list to JSON, we can use toJSON function of jsonlite package. For example, if we have a list called LIST then it can be converted to a JSON by using the command toJSON(LIST,pretty=TRUE,auto_unbox=TRUE). We need to make sure that the package jsonlite is loaded in R environment otherwise the command won’t work.

Example

 Live Demo

List<-list(x1=rnorm(50),x2=rnorm(50),x3=rnorm(50))
List

Output

$x1
[1] 0.94308243 0.92301899 0.38162401 -1.12788003 -1.51346730 -1.13606909
[7] 0.28783669 0.86323673 -0.02449409 -0.13846921 0.15333478 0.21744264
[13] 0.38641702 0.18320168 -1.90449896 0.94474851 -0.01649215 0.50896387
[19] 1.42223991 -0.61505653 1.46317260 -0.60349252 0.17171591 -0.12166583
[25] -1.19631738 0.34294287 -0.84439841 0.42949605 -0.40486289 0.89695985
[31] 0.88799370 0.58950020 0.63531096 -0.10978412 1.11363767 2.36574613
[37] 0.66691232 0.71319992 -1.34867759 -0.37606485 -0.02335364 -0.02595811
[43] 1.08423018 0.52269486 0.61654530 -1.33044218 0.72346125 -0.93851187
[49] 0.49235890 -0.58943233

$x2
[1] -0.44401337 -0.61341264 -0.61251517 -0.05950723 0.91463384 0.90728167
[7] 0.78000181 -0.78143394 1.24853733 -0.85494506 -0.07868259 0.66367854
[13] 0.79670013 -1.33070957 -1.27639339 -0.76427488 1.15823160 0.98859937
[19] 0.02136738 -1.05941847 -0.39919997 1.21333167 -0.31154551 -0.63453177
[25] 1.12919509 0.55256850 -0.32983541 1.07686854 0.06262822 -0.46020131
[31] 1.17276354 -0.51355777 -1.17209858 -1.06453359 -0.70492363 1.51030806
[37] 0.51127388 1.26113160 -0.69110026 -0.69248700 -0.25675307 1.94092037
[43] -1.23696224 0.57922413 0.12468180 1.87270623 -0.15343319 0.36094339
[49] -1.02383545 0.18148960

$x3
[1] 0.13263754 -0.70828647 -0.65796999 -1.43525751 -0.53005370 0.12586719
[7] -0.35785707 -0.79081380 0.35638118 0.48878433 -0.86328512 -0.34382851
[13] -0.48752349 -0.08130422 -0.96386871 2.34599355 1.03778449 -0.48729035
[19] -0.04314423 -0.55603525 -0.63751972 0.23121152 -0.72042376 1.06212143
[25] -0.69914649 0.46835959 0.39862266 -0.72878725 0.92411557 -0.33659477
[31] 0.14468848 -0.26056546 -3.64425038 0.98479249 0.15229069 2.70766937
[37] -1.27364139 -1.80614856 2.33091013 -1.19691045 -0.89934519 -0.50623519
[43] -0.73738808 2.35312730 -0.70206558 1.63775668 -1.01573441 -0.62218434
[49] 0.44641788 2.38678050

Example

library(jsonlite)
ListJSON=toJSON(List,pretty=TRUE,auto_unbox=TRUE)
ListJSON

Output

{
"x1": [0.9431, 0.923, 0.3816, -1.1279, -1.5135, -1.1361, 0.2878, 0.8632, -0.0245, -0.1385, 0.1533, 0.2174, 0.3864, 0.1832, -1.9045, 0.9447, -0.0165, 0.509, 1.4222, -0.6151, 1.4632, -0.6035, 0.1717, -0.1217, -1.1963, 0.3429, -0.8444, 0.4295, -0.4049, 0.897, 0.888, 0.5895, 0.6353, -0.1098, 1.1136, 2.3657, 0.6669, 0.7132, -1.3487, -0.3761, -0.0234, -0.026, 1.0842, 0.5227, 0.6165, -1.3304, 0.7235, -0.9385, 0.4924, -0.5894],
"x2": [-0.444, -0.6134, -0.6125, -0.0595, 0.9146, 0.9073, 0.78, -0.7814, 1.2485, -0.8549, -0.0787, 0.6637, 0.7967, -1.3307, -1.2764, -0.7643, 1.1582, 0.9886, 0.0214, -1.0594, -0.3992, 1.2133, -0.3115, -0.6345, 1.1292, 0.5526, -0.3298, 1.0769, 0.0626, -0.4602, 1.1728, -0.5136, -1.1721, -1.0645, -0.7049, 1.5103, 0.5113, 1.2611, -0.6911, -0.6925, -0.2568, 1.9409, -1.237, 0.5792, 0.1247, 1.8727, -0.1534, 0.3609, -1.0238, 0.1815],
"x3": [0.1326, -0.7083, -0.658, -1.4353, -0.5301, 0.1259, -0.3579, -0.7908, 0.3564, 0.4888, -0.8633, -0.3438, -0.4875, -0.0813, -0.9639, 2.346, 1.0378, -0.4873, -0.0431, -0.556, -0.6375, 0.2312, -0.7204, 1.0621, -0.6991, 0.4684, 0.3986, -0.7288, 0.9241, -0.3366, 0.1447, -0.2606, -3.6443, 0.9848, 0.1523, 2.7077, -1.2736, -1.8061, 2.3309, -1.1969, -0.8993, -0.5062, -0.7374, 2.3531, -0.7021, 1.6378, -1.0157, -0.6222, 0.4464, 2.3868]
}
raja
Published on 06-Mar-2021 12:20:22
Advertisements