title: Convert JSON file to a CSV file using R
body: <p>I have the JSON file in a .txt file which I'm trying to load into R but I'm getting the following error:</p>
  
  <pre><code>Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
    parse error: trailing garbage
        " : "SUCCESS"  }    /* 1 */  {    "_id" : "b736c374-b8ae-4e9
                   (right here) ------^
  </code></pre>
  
  <p>I'm assuming the error is because of multiple instances of /* (number) */ and I can't manually remove them all as my file has 10k instances of these. Is there a way to remove such instances before loading the data into R?</p>
  
  <p>My JSON file looks like below:</p>
  
  <pre><code>/* 0 */
  {
    "_id" : "93ccbdb6-8947",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1KKP",
      "queryId" : "93ccbdb6-8947",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 0,
    "requestDate" : 20151205,
    "totalRecords" : 0,
    "status" : "SUCCESS"
  }
  
  /* 1 */
  {
    "_id" : "b736c374-b8ae",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1KKP",
      "queryId" : "b736c374-b8ae",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 0,
    "requestDate" : 20151205,
    "totalRecords" : 0,
    "status" : "SUCCESS"
  }
  
  /* 2 */
  {
    "_id" : "3312605f-8304",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1SXE",
      "queryId" : "3312605f-8304",
      "subRequests" : [{
          "origin" : "LON",
          "destination" : "IAD",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 2,
    "requestDate" : 20151205,
    "totalRecords" : 0,
    "status" : "SUCCESS"
  }
  
  /* 3 */
  {
    "_id" : "6b668cfa-9b79",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1NXA",
      "queryId" : "6b668cfa-9b79",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 1,
    "requestDate" : 20151205,
    "totalRecords" : 1388,
    "status" : "SUCCESS"
  }
  
  /* 4 */
  {
    "_id" : "41c373a1-e4cb",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP6CXS",
      "queryId" : "41c373a1-e4cb",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 0,
    "requestDate" : 20151205,
    "totalRecords" : 1388,
    "status" : "SUCCESS"
  }
  
  /* 5 */
  {
    "_id" : "2c8331c4-21ca",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1KKP",
      "queryId" : "2c8331c4-21ca",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 0,
    "requestDate" : 20151205,
    "totalRecords" : 1388,
    "status" : "SUCCESS"
  }
  
  /* 6 */
  {
    "_id" : "71a09900-1c13",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP6CXS",
      "queryId" : "71a09900-1c13",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AF",
          "fareClasses" : "",
          "owrt" : "1,2"
        }, {
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }, {
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "DL",
          "fareClasses" : "",
          "owrt" : "1,2"
        }, {
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "LH",
          "fareClasses" : "",
          "owrt" : "1,2"
        }, {
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "BA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 0,
    "requestDate" : 20151205,
    "totalRecords" : 6941,
    "status" : "SUCCESS"
  }
  
  /* 7 */
  {
    "_id" : "a036a42a-918b",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1MMM",
      "queryId" : "a036a42a-918b",
      "subRequests" : [{
          "origin" : "WAS",
          "destination" : "LON",
          "carrier" : "AA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 0,
    "requestDate" : 20151205,
    "totalRecords" : 1388,
    "status" : "SUCCESS"
  }
  
  /* 8 */
  {
    "_id" : "c547be36-805c",
    "uiSearchRequest" : {
      "travelDate" : 20151206,
      "travelDuration" : 7,
      "shopperDuration" : 30,
      "oneWay" : false,
      "userId" : "ATP1SXB",
      "queryId" : "c547be36-805c",
      "subRequests" : [{
          "origin" : "CHI",
          "destination" : "LON",
          "carrier" : "BA",
          "fareClasses" : "",
          "owrt" : "1,2"
        }]
    },
    "downloadCount" : 2,
    "requestDate" : 20151205,
    "totalRecords" : 1072,
    "status" : "SUCCESS"
  }
  </code></pre>
  
  <p>My code is below (Although I haven't gotten much far):</p>
  
  <pre><code>library(jsonlite)
  library(RJSONIO)
  json_data_raw&lt;-fromJSON("mydata.txt")
  
  json_file &lt;- lapply(json_data_raw, function(x) {
    x[sapply(x, is.null)] &lt;- NA
    unlist(x)
  })
  
  output &lt;-- do.call("rbind", json_file)
  write.csv(a, file="json.csv",row.names = FALSE)
  file.show("json.csv")
  </code></pre>
  
  <p>I'm trying to get my output into a CSV file like below</p>
  
  <p><a href="https://i.stack.imgur.com/MJLMn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MJLMn.png" alt="I&#39;m trying to get output into a CSV file like below"></a></p>