0

After converting JSON data into a list using jsonlite, i end up with one of the list looking like following In this case, 10th element contain a list of 9 columns (always fixed) and 2 rows (varies everytime).

mat <- lset$data$comments$data[10]

mat
[[1]]
                                   id can_remove             created_time           from.id
1 10152663742099258_10152663749369258       TRUE 2014-07-01T11:10:29+0000 10203711779968366
2 10152663742099258_10152663842204258       TRUE 2014-07-01T12:15:57+0000         706804257
3 10152663742099258_10152663929639258       TRUE 2014-07-01T13:25:28+0000 10152738599744416
4 10152663742099258_10152663976344258       TRUE 2014-07-01T13:59:33+0000         706804257
         from.name like_count
1      Aileen Yeow          1
2    Tejas Damania          0
3 Sandeep Kulkarni          1
4    Tejas Damania          0
                                                                                                   message
1                                                                                           Lame statement
2 Don't forget, people like you only because they don't know you! <ed><U+00A0><U+00BD><ed><U+00B8><U+00A1>
3      ...for a second I thought it's Accenture Singapore office with some new theme similar to its brand!
4         This is shanghai and nothing to do with firm I work for <ed><U+00A0><U+00BD><ed><U+00B8><U+008E>
  user_likes
1      FALSE
2      FALSE
3       TRUE
4      FALSE

Whole mat shows us as a list of [1] As you can see, it contains list (within a list?). When i print mat then it shows a structure as seen above.

typeof(mat)
[1] "list"

substring(mat,1,100)
[1] "list(id = c(\"10152663742099258_10152663749369258\", \"10152663742099258_10152663842204258\", \"101526637"

I cant access specific elements (say message) from this. Nor I am able to convert this into a matrix of strings so I can access the elements in structured way.

  • Don't use `typeof()`, use `class()`; the former is too low-level to be useful and the latter is really the one that determines behavior. I'm guessing `mat` is actually a data.frame. I would guess `mat[1,"id"]` would extract the first ID element. – MrFlick Aug 24 '14 at 06:43
  • `str()` would give sufficient information on the structure of the list and its elements. – Roman Luštrik Aug 24 '14 at 06:49
  • I think mat is simplified vector. Its not a data.frame, any standard data.frame operations dont work. – Tejas Damania Aug 24 '14 at 07:55
  • Please provide `str(mat)` or `dput(mat)`, otherwise we won't be able to help you – David Arenburg Aug 24 '14 at 10:49
  • Or try even simpler things like `length(mat[[1]])` to see what's in there. – Carl Witthoft Aug 24 '14 at 10:56
  • I changed the fromJSON call parameter to suppress flattening `lset <- fromJSON(jsonobj, simplifyVector = F, flatten=TRUE, unicode = TRUE )` this changes the way mat is formed. I can keep going deeper using $ and find the value only at leaf element! That works for now! thanks for all the help – Tejas Damania Aug 25 '14 at 10:50

1 Answers1

0

I changed the fromJSON call parameter to by setting simplifyVector = FALSE (which is default set to true)

lset <- fromJSON(jsonobj, simplifyVector = F, flatten=TRUE, unicode = TRUE)

this changes the way mat is formed, code maintain nesting all the way down to each leaf element. I can keep going deeper using $ and find the string value only at leaf element! lset$data[[x]]$comments$data[[y]]$from$name

That works for now! thanks for all the help