Say I have this snippet from a large json file:
"name" : "thomas",
"data1": "{\"data\": {\"friends\":[{\"age\": 29, \"married\": yes]}
goes on and on
how would I split it like this instead:
"name" : "thomas",
"friends": "",
"age" : 29,
"married" : "yes"
I know how to split nested lists, but these aren't nested lists, and I can't find anything online about nested object (keys) like this. Thank you!
CodePudding user response:
If it is actually valid json (and your sample above is just typo'd), then
## this code block is *just* to try to programmatically create usable data
json <- jsonlite::toJSON(list(name = "thomas", data = jsonlite::toJSON(list(data = list(friends="", age=29L, married="yes")), auto_unbox=TRUE)), auto_unbox=TRUE)
json
# {"name":"thomas","data":"{\"data\":{\"friends\":\"\",\"age\":29,\"married\":\"yes\"}}"}
Then to parse it, perhaps
outside <- jsonlite::fromJSON(json)
inside <- jsonlite::fromJSON(outside$data)$data
outside$data <- NULL
c(outside, inside)
# $name
# [1] "thomas"
# $friends
# [1] ""
# $age
# [1] 29
# $married
# [1] "yes"