1

Hello I have the following JSON file with 3 observation (each observation called SINGLE) and the last observation is just the SUM.

I would like how to import all the "SINGLE" observation in R:

I've tried the following code:

library("rjson")
json_file <- "my_file.json"
json_data <- fromJSON(file=json_file) 

But my issue is that next I do now know how to format my data.

If I perform this code it won't work

data= as.data.frame(json_data$SINGLE)

I would like to get the "TS" as row and the other variable (page post , user post, etc.) as column.

So I belive I need to find a kind of loop to create the data.frame but I do not know how to do. Someone can help me?

JSON sample:

{"SINGLE":[{"TS":1422777600,"page_posts":8,"user_posts":9,"UM":0,"PT":39,"ST":29,"RE":477,"page_impressions":1703,"clicks":68,"new_likes":10,"virality_pimp_unique":96,"virality_pimp_viral_unique":392,"post_likes":6,"post_shares":0,"post_comments":0},{"TS":1422864000,"page_posts":7,"user_posts":7,"UM":0,"PT":45,"ST":37,"RE":405,"page_impressions":2305,"clicks":52,"new_likes":19,"virality_pimp_unique":127,"virality_pimp_viral_unique":294,"post_likes":6,"post_shares":0,"post_comments":0},{"TS":1424851200,"page_posts":8,"user_posts":16,"UM":0,"PT":135,"ST":51,"RE":1049,"page_impressions":8919,"clicks":200,"new_likes":2,"virality_pimp_unique":729,"virality_pimp_viral_unique":339,"post_likes":23,"post_shares":0,"post_comments":1}],"SUM":{"page_posts":196,"user_posts":232,"UM":5,"PT":3018,"ST":2325,"RE":19112,"page_impressions":117514,"clicks":3714,"new_likes":1124,"virality_pimp_unique":10176,"virality_pimp_viral_unique":9622,"post_likes":710,"post_shares":2,"post_comments":36,"lifetime_likes":2741}}

enter image description here

1 Answer 1

2

The jsonlite package does this automatically:

library(jsonlite)
mydata <- fromJSON("my_file.json")$SINGLE
row.names(mydata) <- mydata$TS
Sign up to request clarification or add additional context in comments.

1 Comment

Dear Jeroen, thanks a lot I ignored the existance of jsonlite

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.