-2

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!

Kowen
  • 11
  • 4
  • 2
    It's not clear what "something like this" means exactly. How id the data stored? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Share the `dput()` of your input do it's clear exactly how your data is formatted. And what is the desired result? Are you trying just to make a single string? – MrFlick Nov 08 '21 at 23:20
  • 1
    Even if we infer that the text after `"data":` is a string that itself contains json, it is still not legal syntax, so I suggest you should find a way to anonymize your data sufficiently to post a *syntactically-valid* json string or we will be less likely to be able to help you. – r2evans Nov 08 '21 at 23:24
  • This is still not valid, unfortunately: `"{\"data" :` seems like the second quote should be escaped, as in `"{\"data\" :`. – r2evans Nov 08 '21 at 23:25
  • I was given the JSON data like this. it's giving me a hard time, because it literally doesn't make sense to how I would go about parsing it and cleaning it. I edited the question a little. – Kowen Nov 08 '21 at 23:26
  • 1
    If they gave it to you in this shape, then *complain loudly*. This is not valid json, so the only way to get it to work is either manual-editing or regex-pain (but really, one should [*never* attempt to use regex with html/xml/json](https://stackoverflow.com/a/1732454/3358272)). – r2evans Nov 08 '21 at 23:28
  • it was given to me like this, yes, also I fixed a minor error that someone pointed out (should be \"data\"). looks like I'm going to have to fix it manually first, unfortunately – Kowen Nov 08 '21 at 23:30

1 Answers1

1

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"
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Kowen, does this work with your real data? – r2evans Nov 09 '21 at 00:43
  • I think so, but I don't know how to use list() since its a file and what I posted was a snippet from that file that I need to fix. I'm going to research now and try to implement this answer! thanks! – Kowen Nov 09 '21 at 13:27
  • *Don't use my `list` code.* That was just there because there are so many concerns with your data. I tried to reproduce what you said you had. If what is there *now* is literally what you have, then this is not JSON, since `{ {...} }` is also invalid (in both your input and your output). Perhaps it's supposed to be `[ {...} ]` instead? Bottom line, if it is supposed to be json, then even if we ignore non-matching quotes, it is still (a) wrong with `{ { ... } }`, and (b) wrong with `\"married\": "yes",` (nothing after the comma). – r2evans Nov 09 '21 at 13:43
  • if I do correct the file, and have it be in correct json format (it was wacky when sent to me), how would I go about splitting it up? I even tried to convert json to csv and work on it this way. I'm open to any and all suggestions. – Kowen Nov 09 '21 at 13:52
  • I think my code suggests ways forward with that. If you need more guidance, then please post "complete/valid" json after your correction (into your OP). – r2evans Nov 09 '21 at 13:54