2

How can I convert this kind of JSON data to data.frame:

library("rjson")
fromJSON("[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]")

Error in fromJSON("[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]") : unexpected character "'"; expecting opening string quote (") for key value when I used fromJSON() I got this:unexpected character "'"; expecting opening string quote (") for key value

4
  • 3
    look into the package called jsonlite Commented Nov 20, 2017 at 2:45
  • 3
    Possible duplicate of Parse JSON with R Commented Nov 20, 2017 at 2:52
  • 3
    a vector is probably not what you want for this data, you probably want a data.frame. Also, you'll need to replace the single-quotes with doubles, and the double-quotes with singles (reference ) Commented Nov 20, 2017 at 2:55
  • this is last error what I got: fromJSON("[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]") Error in fromJSON("[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]") : unexpected character "'"; expecting opening string quote (") for key value Commented Nov 20, 2017 at 3:27

1 Answer 1

3

For JSON to be valid it needs double-quotes inside the JSON string (see this question for reference). Therefore, you also need single-quotes surrounding it to make it a valid string in R.

You can swap your single and double quotes by using that awesome piece of regex supplied by r2evans

## this is your invalid string
js <- "[{'id': 18, 'name': 'Drama'}, {'id': 28, 'name': 'Action'}, {'id': 10749, 'name': 'Romance'}]"

## convert it to a valid string
js <- gsub("QUUX", "'", gsub("'", '"', gsub('"', "QUUX", js)))

Or by making use of the ?chartr function (thanks to thelatemail)

js <- chartr("\'\"","\"\'",js)

Then most JSON parsing libraries should work, for example,

Using jsonlite will give you a data.frame

df <- jsonlite::fromJSON(js)

df
#     id    name
#1    18   Drama
#2    28  Action
#3 10749 Romance

And using rjson will give you a list

rjson::fromJSON(js)

[[1]]
[[1]]$id
[1] 18

[[1]]$name
[1] "Drama"


[[2]]
[[2]]$id
[1] 28

[[2]]$name
[1] "Action"


[[3]]
[[3]]$id
[1] 10749

[[3]]$name
[1] "Romance"
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for answer. I understood what you mean. But my JSON data is written with double quotes , and i don't know how to read this
gsub("QUUX", "'", gsub("'", '"', gsub('"', "QUUX", x)))?
@AzizIlyosov Also, how is your JSON generated?
I downloaded this Data from Internet and I don't know how is that generated
No need for the double regex - just use chartr - fromJSON(chartr("\'\"","\"\'",js))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.