'Error message about unexpected symbol while creating JSON with fromJSON

It's probably an easy thing, but I'm struggling a lot with creating a simple JSON file with a simple text input. Form teh error message it's clear that I'md oding sth. wrong with quotes, but I've no idea what.

library(jsonlite)

# Create an example
jsoncars <- toJSON(mtcars[1:2,], pretty=TRUE)
jsoncars

This gives:

[
  {
    "mpg": 21,
    "cyl": 6,
    "disp": 160,
    "hp": 110,
    "drat": 3.9,
    "wt": 2.62,
    "qsec": 16.46,
    "vs": 0,
    "am": 1,
    "gear": 4,
    "carb": 4,
    "_row": "Mazda RX4"
  },
  {
    "mpg": 21,
    "cyl": 6,
    "disp": 160,
    "hp": 110,
    "drat": 3.9,
    "wt": 2.875,
    "qsec": 17.02,
    "vs": 0,
    "am": 1,
    "gear": 4,
    "carb": 4,
    "_row": "Mazda RX4 Wag"
  }
]

Now, I'm copying this very same text into fromJSON:

fromJSON("[
  {
    "mpg": 21,
    "cyl": 6,
    "disp": 160,
    "hp": 110,
    "drat": 3.9,
    "wt": 2.62,
    "qsec": 16.46,
    "vs": 0,
    "am": 1,
    "gear": 4,
    "carb": 4,
    "_row": "Mazda RX4"
  },
  {
    "mpg": 21,
    "cyl": 6,
    "disp": 160,
    "hp": 110,
    "drat": 3.9,
    "wt": 2.875,
    "qsec": 17.02,
    "vs": 0,
    "am": 1,
    "gear": 4,
    "carb": 4,
    "_row": "Mazda RX4 Wag"
  }
]")

And I'm getting the following error:

Error: unexpected symbol in:
"      {
        "mpg"
Execution halted

BTW, doing fromJSON(jsoncars) works, but it still doesn't solve my problem.

Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source