title: Is there a way to extract multiple attributes efficiently from a JSON column?
body: <p>I have a dataframe that has one column which contains json data. I want to extract some attributes from this json data into named columns of the data frame. </p>
  
  <p>Sample data</p>
  
  <pre><code>json_col = c('{"name":"john"}','{"name":"doe","points": 10}', '{"name":"jane", "points": 20}')
  id = c(1,2,3)
  df &lt;- data.frame(id, json_col)
  </code></pre>
  
  <p>I was able to achieve this using</p>
  
  <pre><code>library(tidyverse)
  library(jsonlite)
  
  extract_json_attr &lt;- function(from, attr, default=NA) {
    value &lt;- from %&gt;% 
               as.character() %&gt;% 
               jsonlite::fromJSON(txt = .) %&gt;%
               .[attr]
  
    return(ifelse(is.null(value[[1]]), default, value[[1]]))
  }
  
  df &lt;- df %&gt;% 
          rowwise() %&gt;%
          mutate(name = extract_json_attr(json_col, "name"),
                 points = extract_json_attr(json_col, "points", 0))
  </code></pre>
  
  <p>In this case the <code>extract_json_attr</code> needs to parse the json column multiple times for each attribute to be extracted. </p>
  
  <p>Is there a better way to extract all attributes at one shot?</p>
  
  <p>I tried this function to return multiple values as a list, but I am not able to use it with mutate to set multiple columns.  </p>
  
  <pre><code>extract_multiple &lt;- function(from, attributes){
    values &lt;- from %&gt;% 
               as.character() %&gt;% 
               jsonlite::fromJSON(txt = .) %&gt;%
               .[attributes]
    return (values)
  } 
  </code></pre>
  
  <p>I am able to extract the desired values using this function</p>
  
  <pre><code>extract_multiple(df$json_col[1],c('name','points'))
  extract_multiple(df$json_col[2],c('name','points')) 
  </code></pre>
  
  <p>But cannot apply this to set multiple columns in a single go. Is there a better way to do this efficiently?</p>