Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can fromJSON coerce type of variable? #416

Open
aamezqui opened this issue Mar 12, 2023 · 3 comments
Open

Can fromJSON coerce type of variable? #416

aamezqui opened this issue Mar 12, 2023 · 3 comments

Comments

@aamezqui
Copy link

I am scraping weather data from Wunderground, and found that jsonlite::fromJson incorrectly maps all temperature data (five variables, which are presented in the webpage with one decimal figure) as integers with (of course) zero decimals. The command recognises however other variables (e.g., rainfall, pressure, latitude) as doubles. Is there any general reason why jsonlite could incur in this wrong mapping? Is it possible define a priori the kind of data for each available "column" in a json file before importing it with fromJSON? I appreciate any help with this issue; I already invested a lot of time looking for possible solutions.

@jeroen
Copy link
Owner

jeroen commented Mar 12, 2023

Can you please include example code so we can see what you mean

@aamezqui
Copy link
Author

Thanks Jeroen. The code is quite simple:

json2dfB <- function(id, date, key) {
u <- paste0("https://api.weather.com/v2/pws/history/all?stationId=",
id,
"&format=json&units=m&date=", date,
"&apiKey=", key)
u %>%
fromJSON() %>%
.$observations %>%
as_tibble() %>%
unnest(cols = c(metric))
}

wuDtTEST <- lapply("IJARDN7", function(x) json2dfB(x, "20230228", apikey)) %>%
bind_rows()

And a glimpse of the downloaded data; temperature should be dbl, as in other variables:
Rows: 288
Columns: 37
$ stationID "IJARDN7", "IJARDN7", "IJARDN7", "IJARDN7", "IJARDN7", "IJARDN7", "IJARD…
$ tz "America/Bogota", "America/Bogota", "America/Bogota", "America/Bogota", …
$ obsTimeUtc "2023-02-28T05:04:52Z", "2023-02-28T05:09:52Z", "2023-02-28T05:14:52Z", …
$ obsTimeLocal "2023-02-28 00:04:52", "2023-02-28 00:09:52", "2023-02-28 00:14:52", "20…
$ epoch 1677560692, 1677560992, 1677561292, 1677561592, 1677561892, 1677562192, …
$ lat 5.496274, 5.496274, 5.496274, 5.496274, 5.496274, 5.496274, 5.496274, 5.…
$ lon -75.887595, -75.887595, -75.887595, -75.887595, -75.887595, -75.887595, …
$ solarRadiationHigh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ uvHigh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ winddirAvg 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, …
$ humidityHigh 98, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, …
$ humidityLow 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, …
$ humidityAvg 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, …
$ qcStatus -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, …
$ tempHigh 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, …
$ tempLow 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, …
$ tempAvg 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, …

@jeroen
Copy link
Owner

jeroen commented Mar 13, 2023

I can't run this code (because I don't have your apikey), so it's hard for me to debug what is going wrong.

Perhaps you can help by further simplifying the example, and creating a small json blob that looks like your input, and then show what jsonlite is doing wrong when we parse it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants