mgloss
mgloss

Reputation: 75

How to parse JSON with strange format in R

I have a JSON, which cannot be transfered even with online parser :-/ I have already tried this, but it gets error

df <- fromJSON("data/part-f1.json", simplifyVector = TRUE, simplifyDataFrame = simplifyVector, simplifyMatrix = simplifyVector, flatten = FALSE)

Error

Error in parse_con(txt, bigint_as_char) : parse error: trailing garbage
      6b967967c8","type":"custom"} {"ts":1512318029084,"userid":"5
                 (right here) ------^

Sample JSON:

{"ts":1512317797740,"userid":"51a35e52c6fba44bd8edba65778c607e","sessionid":"4691122171951068866","remote_ip":"93.72.204.143","platform":"AndroidPlayer","sdk_ver":"u2017.1.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 7.0; XT1650 Build/NPLS25.86-30-5)","submit_time":1512292759927,"name":"Post box activated","custom_params":null,"appid":"4dfb4d07-b463-43d4-ba0b-4c6b967967c8","type":"custom"} {"ts":1512318029084,"userid":"51a35e52c6fba44bd8edba65778c607e","sessionid":"4691122171951068866","remote_ip":"93.72.204.143","platform":"AndroidPlayer","sdk_ver":"u2017.1.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 7.0; XT1650 Build/NPLS25.86-30-5)","submit_time":1512292986741,"name":"Wheel Change","custom_params":{"wheel size":"17","wheel name":"Ratikon"},"appid":"4dfb4d07-b463-43d4-ba0b-4c6b967967c8","type":"custom"} {"ts":1512287567257,"userid":"3996da04b2fef4ac7bf3a47c5b149625","sessionid":"5914000471379591192","remote_ip":"90.178.242.246","platform":"AndroidPlayer","sdk_ver":"u2017.1.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 5.1.1; HUAWEI M2-A01W Build/HUAWEIM2-A01W)","submit_time":1512287567576,"name":"Scene Entered","custom_params":{"scene name":"_Scenes/intro_scene"},"appid":"4dfb4d07-b463-43d4-ba0b-4c6b967967c8","type":"custom"}

So as you can see, in onlineparser I get error multiple roots. I need to make a dataframe from this JSON, but I really need help. Thanks

Upvotes: 3

Views: 2618

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78792

It's also called ndjson and you can use the faster & purpose-built ndjson::stream_in() for it as well.

Upvotes: 3

MrFlick
MrFlick

Reputation: 206167

This isn't really a JSON file, it's a JSONLines file. The difference is that while the entire file itself is not a valid JSON file, each line in the file is a JSON object and each object is separated by a new line character.

You can use the jsonlite::stream_in function to read such data. Something like this should work

jsonlite::stream_in(file("data/part-f1.json"))

Upvotes: 2

Related Questions