Jennifer
Jennifer

Reputation: 163

convert json text entries to a dataframe in r

I have a text file with json like structure that contains values for certain variables as below.

[{"variable1":"111","variable2":"666","variable3":"11","variable4":"aaa","variable5":"0"}]
[{"variable1":"34","variable2":"12","variable3":"78","variable4":"qqq","variable5":"-9"}]

Every line is a new set of values for the same variables 1 through 5. There can be 1000s of lines in a text file but the variables would always remain the same. I want to extract variable 1 through 5 along with their values and convert into a dataframe. Currently I perform these operations in excel using string manipulation and transpose. Here is what it looks like in excel - enter image description here

How to do this in R? Much appreciated. Thanks. J

Upvotes: 0

Views: 37

Answers (1)

Barbara
Barbara

Reputation: 1168

There is a package named jsonlite that you can use.

library("jsonlite")
df<- fromJSON("YourPathToTheFile")

You can find more info here.

Upvotes: 1

Related Questions