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,000
  • 12
  • 95
  • 132
stevejb
  • 2,336
  • 5
  • 25
  • 40
  • 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
  • 27,528
  • 5
  • 43
  • 64
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
  • 30,900
  • 25
  • 101
  • 103