rjsonurlencoderjsonio

Read from JSON where URL contains funny character


I am trying to read from an API where the URL contains a non-standard latin character in R, but am getting an error. Similar URLs without a funny character work fine.

I get following error

> RJSONIO::fromJSON("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbappé")
Error in file(con, "r") : cannot open the connection

Standard characters work fine

res <- RJSONIO::fromJSON("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/neymar")

Solution

  • This is more of an url-encoding problem than an R or JSON one. But this should work:

    RJSONIO::fromJSON("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbapp%C3%A9")
    

    This link could also help you for theoretical background information and this one for practical conversions.

    You can also use utils::URLencode to do the conversion within R:

    utils::URLencode("https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbappé")
    [1] "https://api-prod.footballindex.co.uk/exchange-orders/market-depth/kylian-mbapp%C3%A9"