0

I have the following JSON object:

{"42030636":{"String1":"dixit dominus","String2":"la resurrezione","String3":"water music"}}

After I ingested it using fromJSON() (Jsonlite). I got the following nested list (let's call it list):

$`42030636`$String1
[1] "dixit dominus"

$String2
[1] "la resurrezione"

$String3
[1] "water music"

I'm trying to create a data frame which will look like this:

item_id         String1                  String2             String3
42030636        dixit dominus            la resurrezione     water music

I know that I was able to get the elements in String1:3 using this in a for-loop:

for (i in 1:4) {print(list[1][[1]][1:4]}

The problem is that I can't get the numeric between ` `. It seems that it is at the top level of the JSON block but it can't be "sliced".

When I tried tidyjson package, I couldn't even import the file and it gave me an empty 1x1 tibble. I'm thinking the structure of the original JSON file is off.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139

2 Answers2

0

This can be done with dplyr::bind_rows by specifying .id.

json_data <- jsonlite::fromJSON('{"42030636":{"String1":"dixit dominus","String2":"la resurrezione","String3":"water music"}}')

dplyr::bind_rows(json_data, .id = 'item_id')
#> # A tibble: 1 x 4
#>    item_id       String1         String2     String3
#>      <chr>         <chr>           <chr>       <chr>
#> 1 42030636 dixit dominus la resurrezione water music
Paul
  • 8,734
  • 1
  • 26
  • 36
0

In base r you would do something like:

aggregate(.~ind,stack(json_data),I)
       ind      values.1        values.2    values.3
1 42030636 dixit dominus la resurrezione water music
Onyambu
  • 67,392
  • 3
  • 24
  • 53