1

I have data that I retrieved from a server in JSON format. I now want to pre-process these data in R.

My raw .json file (if opened in a text editor) looks like this:

{"id": 1,"data": "{\"unid\":\"wU6993\",\"age\":\"21\",\"origin\":\"Netherlands\",\"biling\":\"2\",\"langs\":\"Dutch\",\"selfrating\":\"80\",\"selfarrest\":\"20\",\"condition\":1,\"fly\":\"2\",\"flytime\":0,\"purpose\":\"na\",\"destin\":\"Madrid\",\"txtQ1\":\"I\'m flying to Madrid to catch up with friends.\"}"}

I want to parse it back for further use to its intended format:

`{

"id": 1,

"data": {

  "unid": "wU6993",

  "age": "21",

  "origin": "Netherlands",

  "biling": "2",

  "langs": "Dutch",

  "selfrating": "80",

  "selfarrest": "20",

  "condition": 1,

  "fly": "2",

  "flytime": 0,

  "purpose": "na",

  "destin": "Madrid",

  "txtQ1": "I'm flying to Madrid to catch up with friends."

}

}`

Using jsonlite I can't read it in at all:

parsed = jsonlite::fromJSON(txt = 'exp1.json')
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
  lexical error: inside a string, '\' occurs before a character which it may not.
          in\":\"Madrid\",\"txtQ1\":\"I\'m flying to Madrid to catch u
                     (right here) ------^

I think the error tells me that some characters are escaped that should have been.

How can I solve this and read my file?

1 Answer 1

2

You have extra quotes around the nested braces defining "data", the value of which is actually stored as one huge string instead of valid JSON. Take them out, and

my_json <- '{"id": 1,"data": "{\"unid\":\"wU6993\",\"age\":\"21\",\"origin\":\"Netherlands\",\"biling\":\"2\",\"langs\":\"Dutch\",\"selfrating\":\"80\",\"selfarrest\":\"20\",\"condition\":1,\"fly\":\"2\",\"flytime\":0,\"purpose\":\"na\",\"destin\":\"Madrid\",\"txtQ1\":\"I\'m flying to Madrid to catch up with friends.\"}"}'

my_json <- sub('"\\{', '\\{', my_json)
my_json <- sub('\\}"', '\\}', my_json)

jsonlite::prettify(my_json)
# {
#     "id": 1,
#     "data": {
#         "unid": "wU6993",
#         "age": "21",
#         "origin": "Netherlands",
#         "biling": "2",
#         "langs": "Dutch",
#         "selfrating": "80",
#         "selfarrest": "20",
#         "condition": 1,
#         "fly": "2",
#         "flytime": 0,
#         "purpose": "na",
#         "destin": "Madrid",
#         "txtQ1": "I'm flying to Madrid to catch up with friends."
#     }
# }
Sign up to request clarification or add additional context in comments.

Comments

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.