The rjson::fromJSON() reads a file incorrectly while jsonlite::fromJSON() reads it fine. Here's a sample example.
file test.json contents:
{"name": "Sanjay",
"unit_price": 130848,
"amount": 11,
"up_to_data_sales": 45725}
the jsonlite
fromJSON
outputs:
jsonlite::fromJSON("test.json")
$name
[1] "Sanjay"
$unit_price
[1] 130848
$amount
[1] 11
$up_to_data_sales
[1] 45725
But the same throws an error in rjson
package.
rjson::fromJSON("test.json")
Error in rjson::fromJSON("test.json") : parseTrue: expected to see 'true' - likely an unquoted string starting with 't'.
rjson
package was launched when jsonlite
existed?Well:
stringdist::stringdist("rjson", "jsonlite")
## [1] 5
That's a modest difference to begin with.
However, your assertion seems to be amiss:
library(magrittr)
rjson::fromJSON('{"name": "Sanjay",
"unit_price": 130848,
"amount": 11,
"up_to_data_sales": 45725}') %>% str()
## List of 4
## $ name : chr "Sanjay"
## $ unit_price : num 130848
## $ amount : num 11
## $ up_to_data_sales: num 45725
jsonlite::fromJSON('{"name": "Sanjay",
"unit_price": 130848,
"amount": 11,
"up_to_data_sales": 45725}') %>% str()
## List of 4
## $ name : chr "Sanjay"
## $ unit_price : int 130848
## $ amount : int 11
## $ up_to_data_sales: int 45725
Apart from jsonlite
using a more diminutive data type for the numbers, they both parse the JSON fine.
So there's an issue with your file that you failed to disclose in the question.
A further incorrect assertion
-rw-rw-r-- 1 bob staff 2690 Jul 30 2007 rjson_0.1.0.tar.gz
-rw-rw-r-- 1 bob staff 400196 Dec 3 2013 jsonlite_0.9.0.tar.gz
not to mention:
-rw-rw-r-- 1 bob staff 873843 Oct 4 2010 RJSONIO_0.3-1.tar.gz
rjson
came first. (dir listings came from the CRAN mirror sitting next to me).
You can actually read about the rationale and impetus behind jsonlite
here: https://arxiv.org/abs/1403.2805 (which I got off the CRAN page for jsonlite
.
1) Why is the error coming? - Error is due to the mistake in syntax
# For example:
y <- rjson::fromJSON(file = "Input.json")
x <- jsonlite::fromJSON("Input.json")
2) What is the reason rjson package was launched when jsonlite existed?
First, rjson was launched before jsonlite and second, there is a difference in the way they read files:
For example, consider the following input:
{ "id": 1, "prod_info": [ { "product": "xyz", "brand": "pqr", "price": 500 }, { "product": "abc", "brand": "klm", "price": 5000 } ] }
prod_info in the above input is a list with 2 vectors. But jsonlite reads it in the form of dataframe while rjson reads it as a list
Outputs:
x
$id
[1] 1
$prod_info
product brand price
1 xyz pqr 500
2 abc klm 5000
y
$id
[1] 1
$prod_info
$prod_info[[1]]
$prod_info[[1]]$product
[1] "xyz"
$prod_info[[1]]$brand
[1] "pqr"
$prod_info[[1]]$price
[1] 500
$prod_info[[2]]
$prod_info[[2]]$product
[1] "abc"
$prod_info[[2]]$brand
[1] "klm"
$prod_info[[2]]$price
[1] 5000
class(x$prod_info)
[1] "data.frame"
class(y$prod_info)
[1] "list"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With