Unable to parse locally stored JSON file with special character like backslash "\" in R -


i not able parse locally stored json file looks this-

[{"status_code":200,"operation_id":"13-10","response":"{\"emails\": [{\"campaign_id\":\"1111111\",\"email_address\":\"1111@111\",\"activity\": []},{\"campaign_id\":\"22222\",\"email_address\":\"2222@2222\",\"activity\":[]}}}] 

i using jsonlite can see \ present everywhere, , unable parse it. , when -

st<-fromjson("/users/frantr/this_is_r/open_files/json_file.json") print(st) 

i this-

 $ : chr "[{\"status_code\":200"  $ : chr "\"operation_id\":\"13-10\""  $ : chr "\"response\":\"{\\\"emails\\\":    [{\\\"campaign_id\\\":\\\"1111111\\\""  $ : chr "\\\"email_address\\\":\\\"111111111\\\""  $ : chr "\\\"activity\\\":[]}" 

can please help., thank you.

can check how json string generated? there couple of issues it, such " before first internal { (after response:), , missing ].

then, suspected \ causing issue. i'm using readlines , removing \ in 1 step. json can read using jsonlite::fromjson

for example, have file on desktop called myjson.json, contains string

[{"status_code":200,"operation_id":"13-10","response":{\"emails\":[{\"campaign_id\":\"1111111\",\"email_address\":\"1111@111\",\"activity\":[]},{\"campaign_id\":"22222\",\"email_address\":\"2222@2222\",\"activity\":[]}]}}] 

i can read in using

lst <- fromjson(gsub("\\\\","",readlines("~/desktop/myjson.json")))  str(lst) # 'data.frame': 1 obs. of  3 variables: #   $ status_code : int 200 # $ operation_id: chr "13-10" # $ response    :'data.frame':  1 obs. of  1 variable: #   ..$ emails:list of 1 # .. ..$ :'data.frame': 2 obs. of  3 variables: #   .. .. ..$ campaign_id  : chr  "1111111" "22222" # .. .. ..$ email_address: chr  "1111@111" "2222@2222" # .. .. ..$ activity     :list of 2 # .. .. .. ..$ : list() # .. .. .. ..$ : list() 

you can whatever components/data want list

lst$response$emails  # [[1]] # campaign_id email_address activity # 1     1111111      1111@111     null # 2       22222     2222@2222     null 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -