Comments (11)
The json
format unfortunately does not natively support dates or timestamps. BSON does, but that is another story. jsonlite
gives you several options on how to encode dates and times:
> now <- Sys.time()
> toJSON(now)
[1] "[ \"2014-02-23 14:14:40\" ]"
> toJSON(now, POSIXt="ISO8601")
[1] "[ \"2014-02-23T14:14:40\" ]"
> toJSON(now, POSIXt="epoch")
[1] "[ 1393193680926 ]"
> toJSON(now, POSIXt="mongo")
[1] "[ { \"$date\" : 1393193680926 } ]"
And for Date
:
> today <- as.Date(now)
> toJSON(today)
[1] "[ \"2014-02-23\" ]"
> toJSON(today, Date="epoch")
[1] "[ 16124 ]"
The fromJSON
parser will not automatically try to detect dates. This excel like hacking is too dangerous and can lead to unexpected behavior. However the base R functions as.Date
and as.POSIXct
are actually very good at coersing strings into dates and timestamps:
as.Date(fromJSON(toJSON(today)))
as.POSIXct(fromJSON(toJSON(now)))
from jsonlite.
Thought I'd post this here for anyone looking to make that final step of getting the json'd date into an R date format. http://stackoverflow.com/questions/15838548/parsing-iso8601-date-and-time-format-in-r
from jsonlite.
I agree with the notion that excel like date coercion is pretty dangerous, except for the POSIXt="mongo" case. There you have a clear type definition that you have a date object and what the value is. It would be very nice for a fromJSON call to allow a POSIXt="mongo" and handle the special mongodb dates.
from jsonlite.
@johnwebbcole that sounds reasonable. I tried to implement this. Would you mind testing if this is doing what you would expect?
x <- Sys.time() + 1:3
fromJSON(toJSON(x, POSIX="mongo"))
from jsonlite.
@jeroenooms I tried loading from github:
> dev_mode(on=T)
Dev mode: ON
d> install_github("jsonlite")
Installing github repo jsonlite/master from hadley
Downloading master.zip from https://github.com/hadley/jsonlite/archive/master.zip
Error in (function (url, name = NULL, subdir = NULL, config = list(), :
client error: (404) Not Found
How should I test that?
Looking at it, I would think I need to pass in an option to fromJSON to let it know that it's mongo flavor.
query <- fromJSON('{
"ts": {
"$gt": { "$date": 1394598852736 }
}
}', POSIX="mongo")
from jsonlite.
You would do devtools::install_github("jeroenooms/jsonlite”)
, otherwise
it defaults to hadley’s repository.
On 16 Apr 2014, at 16:49, johnwebbcole [email protected] wrote:
@jeroenooms I tried loading from github:
dev_mode(on=T)
Dev mode: ON
d> install_github("jsonlite")
Installing github repo jsonlite/master from hadley
Downloading master.zip from https://github.com/hadley/jsonlite/archive/master.zip
Error in (function (url, name = NULL, subdir = NULL, config = list(), :
client error: (404) Not Found
How should I test that?Looking at it, I would think I need to pass in an option to fromJSON to let it know that it's mongo flavor.
query <- fromJSON('{
"ts": {
"$gt": { "$date": 1394598852736 }
}
}', POSIX="mongo")
—
Reply to this email directly or view it on GitHub.
from jsonlite.
@rubenarslan Thanks, that worked.
Ok running this:
x <- Sys.time() + 1:3
print(toJSON(x, POSIX="mongo"))
print(toJSON(x))
produces
[1] "[ { \"$date\" : 1397660463360 }, { \"$date\" : 1397660464360 }, { \"$date\" : 1397660465360 } ]"
[1] "[ \"2014-04-16 10:01:03\", \"2014-04-16 10:01:04\", \"2014-04-16 10:01:05\" ]"
This seems ok to me. However
query <- fromJSON('{
"ts": {
"$gt": { "$date": 1397661867620 }
}
}')
print(query)
print(toJSON(query, POSIX="mongo"))
print(toJSON(query))
produces
$date
1 1.394599e+12
[1] "[ { \"$gt\" : { \"$date\" : { \"ts\" : [ 1397661867620 ] } } } ]"
[1] "[ { \"$gt\" : { \"$date\" : { \"ts\" : [ 1397661867620 ] } } } ]"
I expected
ts : 3
$gt : 9 1397661867620
[1] "[ { \"$ts\" : { \"$gt\" : { \"$date\" : 1397661867620 } } } ]"
[1] "[ { \"$ts\" : { \"$gt\" : \"2014-04-16 10:24:27\" } } ]"
from jsonlite.
Yeah it only works for mongo style data which should appear within a json array
myjson <- '[
{ "ts": { "$gt": { "$date": 1397661867620 } } },
{ "ts": { "$gt": { "$date": 1397661867621 } } },
{ "ts": { "$gt": { "$date": 1397661867622 } } }
]'
fromJSON(myjson)
from jsonlite.
I pushed another update. Can you try again?
from jsonlite.
query <- fromJSON('{
"ts": {
"$gt": { "$date": 1394598852736 }
}
}')
print(query)
print(toJSON(query, POSIX="mongo"))
print(toJSON(query))
produces
$ts
$ts$`$gt`
[1] "2014-03-11 23:34:12 CDT"
[1] "{ \"ts\" : { \"$gt\" : [ { \"$date\" : 1394598852736 } ] } }"
[1] "{ \"ts\" : { \"$gt\" : [ \"2014-03-11 23:34:12\" ] } }"
which is pretty close to what I expect, except for $gt being inside an array.
from jsonlite.
That is actually expected because R does not have scalars, so the $gt
value is a vector. You can disable this using either the unbox
function, or the auto_unbox
argument.
from jsonlite.
Related Issues (20)
- Hardcoded 15 significant digits in num_to_char.c HOT 14
- Support decompression in `jsonlite::read_json` HOT 2
- One error in tests on PowerPC: test-serializeJSON-S4.R
- Can fromJSON coerce type of variable? HOT 3
- [Documentation] Is toJSON's output minified by default? HOT 1
- jsonlite throws SSL connect error through firewall
- spaces and parenthesis in Json Keys names converted to dots. HOT 4
- Behaviour of `toJSON` with `digits=NA` changed from 1.8.4 -> 1.8.5 HOT 2
- Update tests to newer testthat approach HOT 2
- `toJSON()` enters infinite recursion for `labelled` vectors HOT 2
- Error (data_api.R#313): lexical error: invalid char in json text. HOT 1
- yajl vulnerability: 'yajl_tree_parse' memory leak HOT 2
- Adding "undefined" as option to `na` in `toJSON` HOT 3
- Please enable usage of system yajl library HOT 2
- embedded yajl is vulnerable to CVE-2017-16516 and CVE-2022-24795 HOT 3
- Mutating & erring S3-dispatch attempts
- Title for read_json not consistent with rest of documentation like toJSON, fromJSON
- Negative zeros HOT 2
- `fromJSON()` with nested array
- Option for toJSON() to output a decimal point for all doubles
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonlite.