2

I am using R with jsonlite to read in a JSON file like this:

{
    "VEVENT": [
    {
        "DTSTAMP": "20150608T021037Z",
        "DTSTART;TZID=America/Los_Angeles": "20150608T173000",
        "DTEND;TZID=America/Los_Angeles": "20150608T183000",
        "STATUS": "CONFIRMED",
        "SUMMARY": "Meeting ABC",
        "DESCRIPTION": "Line 1\nLine 2\nLine 3"
    }
    ]
}

Say I have access to the description string as the variable f, then

writelines(f) outputs Line 1\nLine 2\nLine 3. How can I modify f to output

Line 1
Line 2
Line 3
josliber
  • 43,891
  • 12
  • 98
  • 133
stevejb
  • 2,414
  • 5
  • 26
  • 41
  • 1
    What exactly is in `f`? Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also, do you mean `writeLines()` rather than `writelines()`? If you don't have a vector, consider using `cat()` instead. – MrFlick Jun 08 '15 at 04:20
  • I was unable to load your JSON string using `fromJSON()` because R complained about the carriage returns (\n). – Tim Biegeleisen Jun 08 '15 at 04:22

2 Answers2

1

More info should be added, but here's a start,

x <- 'Line 1\nLine 2\nLine 3'
cat(x)
Line 1
Line 2
Line 3
Pierre L
  • 28,203
  • 6
  • 47
  • 69
0

After dropping that text into test.json, I can do this:

library(jsonlite)
x <- data.frame(fromJSON("~/Desktop/test.json", flatten = TRUE))
cat(x$VEVENT.DESCRIPTION)

to get:

Line 1
Line 2
Line 3

then write to file with:

file.create("description.txt")
fileConn <- file("description.txt")
writeLines(x$VEVENT.DESCRIPTION, fileConn)
Maiasaura
  • 32,226
  • 27
  • 104
  • 108