title: Extracting values from lists in json file using R
body: <p>I am quite inexperienced in using R and I have to complete a script for a school project.<br>
  I have a json file with nested lists and I have to retrieve values contained in two attributes. The problem is that these attributes are in different lists inside my file. So I tried this:</p>
  
  <pre><code>`grabInfo&lt;-function(var){
     sapply(json_data.rows, function(x) returnData(x, var)) 
  }
  returnData&lt;-function(x, var){
       if(!is.null( x$doc$payload[[var]])){
             return( trim(x$doc$payload[[var]]))
         }else if(!is.null(x$doc$sensorType == 'movement')){
              return( trim( rbind(x$doc$value, x$doc$date_inmilli)))
          }else {
               return(NA)
           }
  }
  fmDataDF&lt;-data.frame(sapply(c(1,3), grabInfo), stringsAsFactors=FALSE)`
  </code></pre>
  
  <p>In the first <code>if</code> (so the first kind of list) I extract values contained in <code>var</code> (in particular the first and third attribute). When it comes to the second list, I have to check if it is the right sensor (data are dirty), but I cannot use the var because "value" and "date_inmilli" are in different positions (<code>c(3,5)</code>, so I had to code directly where to find them...I know...horrible) and in my dataframe instead of having two values in the two columns, I have two vectors (value, date) for each column.</p>
  
  <p>Could you help me fix this?  I hope I was clear enough.</p>
  
  <p>An example with just 2 rows of my file json_data (from which I made a subset<code>json_data.rows &lt;- json_data[['rows']]</code> used in the script) is showed below:</p>
  
  <p><code>{"total_rows":99019,"offset":0,"rows":[
  {"id":"5f238411f1d877723c8e5f7a40c1fa3f","key":"5f238411f1d877723c8e5f7a40c1fa3f","value":{"rev":"1-7625857a2f79af0e46c2b73fdbde986e"},"doc":{"_id":"5f238411f1d877723c8e5f7a40c1fa3f","_rev":"1-7625857a2f79af0e46c2b73fdbde986e","value":0,"date_human":"2017-04-12T03:44:20.803Z","date_inmilli":1491968660803,"sensorType":"movement","date":"2017-04-12T03:44:19.902Z"}},
  {"id":"0006d0a04d14c4cf0158db1a9a185dac","key":"0006d0a04d14c4cf0158db1a9a185dac","value":{"rev":"1-496c4d06dff82c1ad8a03cbcdf47f10b"},"doc":{"_id":"0006d0a04d14c4cf0158db1a9a185dac","_rev":"1-496c4d06dff82c1ad8a03cbcdf47f10b","topic":"iot-2/type/node-red-wiotp/id/f5f7aa27.368c48/evt/update/fmt/json","payload":{"value":0,"date_human":"2017-04-20T17:55:43.788Z","date_inmilli":1492710943788,"sensorType":"movement"},"deviceId":"f5f7aa27.368c48","deviceType":"node-red-wiotp","eventType":"update","format":"json","increment":0}}]}
  </code></p>
  
  <p>On sample output is this:</p>
  
  <p><code>Values                 Date
  93       0                  1493750501405
  94 c(1, 1491568686336)  c(1, 1491568686336)
  </code>       </p>