0

I need to transform a json table to lines of json:

To go from:

[{"field1": "ABC","field2": "DEF"},
{"field1": "GHI","field2": "JKL"}]

To an plain-text file where each line is valid json, like:

{"field1": "ABC","field2": "DEF"}
{"field1": "GHI","field2": "JKL"}

I realize I could do this through a regular expression, but would rather use an existing method if one exists.

Found http://jsonlines.org/ , which is what I'm trying to do, but didn't see a package for it.

Any suggestions?

sheldonzy
  • 5,505
  • 9
  • 48
  • 86

1 Answers1

4

Try the jsonlite library:

library(jsonlite)
text <- '[{"field1": "ABC","field2": "DEF"}, {"field1": "GHI","field2": "JKL"}]'
x <- fromJSON(text) # convert to data.frame
stream_out(x, file("file.json")) # save as newline-delimited JSON
Patrick Perry
  • 1,422
  • 8
  • 17