Open mattpollock opened 6 years ago
This is expected. You cannot convert attribute_counts
to a tibble because it has nested data. There is no obvious way to flatten it into a rectangle.
One thing you can do is convert the nested fields:
out <- jsonlite::fromJSON(json)
tibble::as.tibble(out$attribute_counts$color)
tibble::as.tibble(out$attribute_counts$size)
tibble::as.tibble(out$attribute_counts$name)
But for such complex data you may be better of using simplifyVector=FALSE
and manually writing code to convert the data into the required structure.
Thanks for the quick response. Unfortunately I'm after a more general solution (I have lots of different data types that combine nested arrays and non-array objects at the top level which I need to work with).
I see your point about not being able to coerce everything into a rectangle, but given the relationship between data.frame
rows and top level json objects, it seems to me that leaving attribute_counts
(and similar structures) as list
columns would be more usable than the current return value. Thus, df$attribute_counts
would be a list
of length nrow(df)
and df$attribute_counts[[1]]
would be a list
(not a data.frame
) containing the attribute counts from the original json corresponding to item 1.
Perhaps I should ask, is the behavior here expected because it is useful in another context or is it expected because jamming all json objects into data.frame
s doesn't always work?
I need to parse json that has the following form:
The problem is with the
attribute_counts
field. It seems that since this is an object, but not an array of objects (likepoints
is, whichfromJSON
handles with ease) it does not get lined up according to row (i.e., in thedata.frame
row corresponding to the top level object it comes from) but rather the attribute counts from all top level fields arerbind
ed together in a way that does not seem intentional.All is well if I use
simplifyVector=FALSE
(except that this data would be much easier to work with as adata.frame
- but from a "correctness" perspective)The transpose of this also looks good
But things go wrong when I use
simplifyVector=TRUE
The
attribute_counts
for both items 1 and 2 are lumped together in their owndata.frame
. The column breaks what adata.frame
is since the column lengths do not match.Isolating the
attribute_counts
data.frame
does not help either because each field contains adata.frame
instead of a list of nesteddata.frame
s. Basedata.frame
will print it in an unhelpful way, buttibble::as.tibble
anddata.table::as.data.table
won't work with it.I'm not sure what the correct behavior ought to be, but either parsing the
attribute_counts
for each item into a 1-rowdata.frame
or leaving it as a nestedlist
seem like fine options to me.Thanks