DanyAlejandro
DanyAlejandro

Reputation: 1468

How can I print a list of named lists from a JSON response in R?

I'm writing a script in R that consumes a web API, which returns data in a big array of complex JSON objects, similar to:

[
    {
        "runIds": [9505160, 89098],
        "fileCount": 2,
        "runTime": 1.469,
        "requestCount": 1
    },
    {
        "runIds": [12, 13, 14],
        "fileCount": 1,
        "runTime": 2.9,
        "requestCount": 10
    }
]

I'm using the httr R package to do a GET request, which grabs this data and returns it as a list of lists. In case you're unfamiliar with the response format, check the output of this dummy R example:

library(httr)
response = GET("https://jsonplaceholder.typicode.com/users")
content(response)

Which prints something like:

[[10]]$company
[[10]]$company$name
[1] "Hoeger LLC"

[[10]]$company$catchPhrase
[1] "Centralized empowering task-force"

[[10]]$company$bs
[1] "target end-to-end models"
(...)

The printed response is hard to read. Is there any way to print this list of lists (of lists etc.) in a "prettified" way in R?

Upvotes: 2

Views: 673

Answers (1)

maydin
maydin

Reputation: 3755

With jsonlite package, you can transform it to a dataframe.

library(httr)

   response = GET("https://jsonplaceholder.typicode.com/users")

   my_json_text <-content(response,as="text") 

library(jsonlite)

   my_df_output <- fromJSON(my_json_text)

   head(my_df_output,2)

which gives,

  id          name  username             email address.street address.suite address.city address.zipcode address.geo.lat address.geo.lng                 phone       website    company.name
1  1 Leanne Graham      Bret [email protected]    Kulas Light      Apt. 556  Gwenborough      92998-3874        -37.3159         81.1496 1-770-736-8031 x56442 hildegard.org Romaguera-Crona
2  2  Ervin Howell Antonette [email protected]  Victor Plains     Suite 879  Wisokyburgh      90566-7771        -43.9509        -34.4618   010-692-6593 x09125 anastasia.net    Deckow-Crist
                     company.catchPhrase                       company.bs
1 Multi-layered client-server neural-net      harness real-time e-markets
2         Proactive didactic contingency synergize scalable supply-chains

EDIT:

If the file includes list in lists, we can actually do the same thing as well like,

data <- '[
    {
        "runIds": [9505160, 89098],
        "fileCount": 2,
        "runTime": 1.469,
        "requestCount": 1
    },
    {
        "runIds": [12, 13, 14],
        "fileCount": 1,
        "runTime": 2.9,
        "requestCount": 10
    }
]'

 data2 <- fromJSON(data)

If we call data2,

data2
          runIds fileCount runTime requestCount
1 9505160, 89098         2   1.469            1
2     12, 13, 14         1   2.900           10


sapply(data2,class)

      runIds    fileCount      runTime requestCount 
      "list"    "integer"    "numeric"    "integer" 

It is in list format. So we can store it as list format inside our dataframe which is more prettified comparing to the content() output or we can prefer to transform it into a pure dataframe by ,

library(tidyverse)

data2 %>% unnest(runIds)

which gives,

  fileCount runTime requestCount  runIds
1         2   1.469            1 9505160
2         2   1.469            1   89098
3         1   2.900           10      12
4         1   2.900           10      13
5         1   2.900           10      14

Upvotes: 2

Related Questions