I am not at all an expert about Json files and I am struggling to complete a simple task. Consider the json file below stored in test.json
{
"entry_1": {
"AT": null,
"BE": null,
"BG": null,
"CY": null,
"CZ": null,
"DE": null,
"DK": null,
"EE": null,
"EL": null,
"ES": null,
"FI": null,
"FR": null,
"HR": null,
"HU": null,
"IE": null,
"IT": null,
"LT": null,
"LU": null,
"LV": null,
"MT": null,
"NL": null,
"PL": null,
"PT": null,
"RO": null,
"SI": null,
"SK": null
},
"entry_2": {
"AT": null,
"BE": null,
"BG": null,
"CY": null,
"CZ": null,
"DE": null,
"DK": null,
"EE": null,
"EL": null,
"ES": null,
"FI": null,
"FR": null,
"HR": null,
"HU": null,
"IE": null,
"IT": null,
"LT": null,
"LU": null,
"LV": null,
"MT": null,
"NL": null,
"PL": null,
"PT": null,
"RO": null,
"SI": null,
"SK": null
},
"entry_3": {
"AT": null,
"BE": null,
"BG": null,
"CY": null,
"CZ": null,
"DE": null,
"DK": null,
"EE": null,
"EL": null,
"ES": null,
"FI": null,
"FR": null,
"HR": null,
"HU": null,
"IE": null,
"IT": null,
"LT": null,
"LU": null,
"LV": null,
"MT": null,
"NL": null,
"PL": null,
"PT": null,
"RO": null,
"SI": null,
"SK": null
},
"entry_4": {
"AT": null,
"BE": null,
"BG": null,
"CY": null,
"CZ": null,
"DE": null,
"DK": null,
"EE": null,
"EL": null,
"ES": null,
"FI": null,
"FR": null,
"HR": null,
"HU": null,
"IE": null,
"IT": null,
"LT": null,
"LU": null,
"LV": null,
"MT": null,
"NL": null,
"PL": null,
"PT": null,
"RO": null,
"SI": null,
"SK": null
},
"entry_5": {
"AT": null,
"BE": null,
"BG": null,
"CY": null,
"CZ": null,
"DE": null,
"DK": null,
"EE": null,
"EL": null,
"ES": null,
"FI": null,
"FR": null,
"HR": null,
"HU": null,
"IE": null,
"IT": null,
"LT": null,
"LU": null,
"LV": null,
"MT": null,
"NL": null,
"PL": null,
"PT": null,
"RO": null,
"SI": null,
"SK": null
}
}
I would like to read it in my R session and then modify its entries and save it back as a new Json file. Based on the suggestions I received, I paste below a revised reprex
library(tidyverse)
library(jsonlite)
#>
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#>
#> flatten
entry_1 <- tibble(x=c("AT", "IT", "HU"), value=c(12, 0.9, 4)) |>
pivot_wider(names_from= x, values_from=value)
entry_2 <- tibble(x=c("FR", "IE", "RO"), value=c(1.2, 0.9, 4.8))|>
pivot_wider(names_from= x, values_from=value)
entry_3 <- tibble(x=c("DE", "FI", "EL"), value=c(1.7, 0.09, 4.7))|>
pivot_wider(names_from= x, values_from=value)
entry_4 <- tibble(x=c("SK", "LT", "BG"), value=c(1.8, 0.967, 4.6))|>
pivot_wider(names_from= x, values_from=value)
entry_5 <- tibble(x=c("FR", "IT", "IE"), value=c(129, 9.4, 4.3))|>
pivot_wider(names_from= x, values_from=value)
newdata <- list(entry_1, entry_2, entry_3, entry_4, entry_5)
names(newdata) <- c("entry_1", "entry_2", "entry_3", "entry_4", "entry_5")
newdataJSON<-toJSON(newdata, pretty=TRUE, auto_unbox = TRUE)
newdataJSON
#> {
#> "entry_1": [
#> {
#> "AT": 12,
#> "IT": 0.9,
#> "HU": 4
#> }
#> ],
#> "entry_2": [
#> {
#> "FR": 1.2,
#> "IE": 0.9,
#> "RO": 4.8
#> }
#> ],
#> "entry_3": [
#> {
#> "DE": 1.7,
#> "FI": 0.09,
#> "EL": 4.7
#> }
#> ],
#> "entry_4": [
#> {
#> "SK": 1.8,
#> "LT": 0.967,
#> "BG": 4.6
#> }
#> ],
#> "entry_5": [
#> {
#> "FR": 129,
#> "IT": 9.4,
#> "IE": 4.3
#> }
#> ]
#> }
Created on 2022-09-27 by the reprex package (v2.0.1)
which comes very close to what I need. One thing only bothers me: is there any way to get rid of all the square brackets in the final Json file?