Converting R list to JSON Converting R list to JSON mongodb mongodb

Converting R list to JSON


Use gsub()

jsontest <- gsub(pattern = '^\[', replacement = "", x = jsontest)jsontest <- gsub(pattern = '\]$', replacement = "", x = jsontest)

Results :

  {    "section_id": {},    "name": "name1",    "slug": "slug1"  },  {    "section_id": {},    "name": "name2",    "slug": "slug2"  },  {    "section_id": {},    "name": "name3",    "slug": "slug3",    "categories": [      {        "section_id": {},        "name": "name31",        "slug": "slug31"      },      {        "section_id": {},        "name": "name32",        "slug": "slug32"      }    ]  }

Still json class :

> class(jsontest)[1] "json"

But maybe the blank space might cause some troubles.


Using the mongolite package you should be able to simply use the insert() function to insert it directly into the collection.