1

I have a named list in R that I want to convert to JSON (in order to send it as a document in MongoDB)

L=list(A=1,B=as.POSIXct("1990-01-01"))

If I do simply toJSON, the date format of the second element is lost (it is converted to numeric).

 > cat(toJSON(L))
 {
   "A":      1,
   "B": 6.3115e+08 
 }

How can I can obtain:

{
 "A":      1,
 "B": new Date('1990-01-01')
}

?

I have not found anything like this in the documentation of JSONIO package.

RockScience
  • 17,932
  • 26
  • 89
  • 125

1 Answers1

0

Try converting the date to ISO format and with the $date attribute, i think you can use strptime such that it looks like:

{ "B": { "$date" : "2012-05-15T00:16:15.184Z"} }
tommy chheng
  • 9,108
  • 9
  • 55
  • 72