title: How to fix "invalid char in json text" when passing a URL via function to dplyr:mutate
body: <p>I have written a function to retrieve data via API. The output format is JSON. <a href="https://jsoneditoronline.org/?id=ac0ec7ececae49ca92599ff912458a84" rel="nofollow noreferrer">https://jsoneditoronline.org/?id=ac0ec7ececae49ca92599ff912458a84</a> </p>
  
  <p>With every query a variable (path) should change. This variable is located in a dataframe (product_folders_summarised) in the column (product_folder).</p>
  
  <pre><code>library(tidyverse)
  library(httr)
  library(jsonlite)
  library(data.table)
  
  func_visibility &lt;- function(product_folder) {
    api_url &lt;- "https://api.where-the-data-comes-from.com/example"
    api_key &lt;- "_API_KEY_"
    format &lt;- "json"
    request &lt;-
      fromJSON(
        paste0(api_url, "?api_key=",api_key,"&amp;format=",format,"&amp;path=",product_folder),
        simplifyVector = TRUE,
        simplifyDataFrame = TRUE,
        flatten = TRUE
      )
    request &lt;- lapply(request, function(x) {
      x[sapply(x, is.null)] &lt;- NA
      unlist(x)
    })
    request &lt;- as.data.frame(t(request$answer))
    request &lt;- select(request, -sichtbarkeitsindex.path, -sichtbarkeitsindex.date)
    return(request) 
  }
  
  product_folders_summarised &lt;- product_folders_summarised %&gt;%
    dplyr::mutate(visibility_value = func_visibility(product_folder))
  </code></pre>
  
  <p>The dataframe is structured as follows:</p>
  
  <pre><code>|product_folder|value_1|value_2|
  |https://www.example.de/folder/|this|that|
  |https://www.example.de/anotherfolder/|...|...|
  </code></pre>
  
  <p>I expect that from the dataframe (product_folders_summarised) the value is taken from the column (product_folder), passed to the function and visibility_value is added as column.</p>
  
  <p>Instead I get the error message </p>
  
  <pre><code>Error: lexical error: invalid char in json text.
                                         https://api.https://api.where-the-data-comes-from.com/example.
                       (right here) ------^
  </code></pre>
  
  <p>I have now adjusted my function as suggested by r2evans.</p>
  
  <pre><code>func_visibility &lt;- function(path) {
    api_url &lt;- "https://api.where-the-data-comes-from.com/example"
    api_key &lt;- "_API_KEY_"
    format &lt;- "json"
    request &lt;- paste0(api_url,"?api_key=",api_key,"&amp;format=",format,"&amp;path=",path)
    request &lt;- lapply(request, jsonlite::fromJSON)
    request &lt;- lapply(request, function(x) {
      x[sapply(x, is.null)] &lt;- NA
      as.data.frame(t(x))  
      unlist(x)
    })
    return(request)
  }
  product_folders_summarised_short &lt;- product_folders_summarised_short %&gt;%
    dplyr::mutate(sichtbarkeitsindex_value = func_visibility(product_folder))
  </code></pre>
  
  <p>The data is now retrieved from the API. The data is written into the new last column of the dataframe:</p>
  
  <pre><code>c(method = "domain.sichtbarkeitsindex", answer.sichtbarkeitsindex.path = "https://www.example.de/folder/", answer.sichtbarkeitsindex.date = "2019-09-02T00:00:00+02:00", answer.sichtbarkeitsindex.value = "0", credits.used = "1")
  </code></pre>
  
  <p>In my first attempt (see first codeblock), I converted the data into a dataframe.</p>
  
  <pre><code>request &lt;- as.data.frame(t(request$answer)),
  request &lt;- select(request, -sichtbarkeitsindex.path, -sichtbarkeitsindex.date),
  </code></pre>
  
  <p>Applied to a single URL, this worked. Now I integrated</p>
  
  <pre><code>`as.data.frame(t(x))`,
  </code></pre>
  
  <p>but I only get the result that the data from the API is stored as a character vector.</p>
  
  <p>Do you think it is easier to write the data as a character vector in the last column of the dataframe, in order to assign the vectors to a new dataframe after passing the first function with another function?</p>