Pioggia_91
Pioggia_91

Reputation: 13

fromJSON data types

I am trying to use jsonlite package and I have an issue with fromJSON function. I read https://arxiv.org/pdf/1403.2805.pdf and and documentation at https://cran.r-project.org/web/packages/jsonlite/index.html but I still don't understand the point...

Consider the following json file named JsonFileOne.json:

{"cars": [ {"models" : [ {"type":"Fiesta"} ] } ] } 

When I try command

data<-fromJSON(“JsonFileOne.json”) 

I have that the first array, “cars”, is read as an R data.frame while the second, “models”, is an R list. In addition the object “type” is a list with no names as names(data$cars$models) is NULL.

Why is the first array read as a data.frame and the second one as a list? What is the criterion used by fromJSON to assign an R data type to json entities? Why the object inside the second array is not named “type” but has on the contrary no name?

Upvotes: 1

Views: 415

Answers (1)

mw0000
mw0000

Reputation: 59

'models' as a list contains data frame in [[1]], so you can get a column name 'type'.

names(data$cars$models[[1]])
[1] "type"

data$cars$models[[1]]
    type
1 Fiesta

The object in column 'type' is not a list. 'Fiesta' is an observation for a variable 'type' and can be accessed like that:

data$cars$models[[1]][1,]
[1] "Fiesta"

or

data$cars$models[[1]]$type
[1] "Fiesta"

Upvotes: 1

Related Questions