-1

My string is as below.

[{\"period\":\"01-06-2018\",\"count\":5},{\"period\":\"01-07-2018\",\"count\":8},{\"period\":\"01-08-2018\",\"count\":9}]

but I want only (only backslash) to be removed and it should look like below (using R programming functions)

[{"period":"01-06-2018","count":5},{"period":"01-07-2018","count":8},{"period":"01-08-2018","count":9}]
s__
  • 9,270
  • 3
  • 27
  • 45
  • 2
    1) This looks like JSON content, and therefore you should probably _not_ be using regex to manipulate it. 2) Those backslashes in front of the double quotes are probably only there for display purposes, to show that they are _literal_ double quotes, and not delimiters marking the start and end of a string. – Tim Biegeleisen Dec 20 '18 at 11:09
  • I have a dataframe and I need to convert it to json, how to do that. How ever I used x <- toJSON(unname(split(dataframeName, 1:nrow(dataframeName)))) but I am getting that json including those backslash. so I want to remove those backslashes – Subham Chand Dec 20 '18 at 11:12
  • `gsub("\","",your.string)`? – user2974951 Dec 20 '18 at 11:13
  • @user2974951 they would need four backslashes. And it wouldn't work. – NelsonGon Dec 20 '18 at 11:14

1 Answers1

0

This should do it:

library(tidyverse)
somestring<-c("[{\"period\":\"01-06-2018\",\"count\":5},{\"period\":\"01-07-2018\",\"count\":8},{\"period\":\"01-08-2018\",\"count\":9}]")

library(jsonlite)
fromJSON(somestring)

This yields:

   period count
1 01-06-2018     5
2 01-07-2018     8
3 01-08-2018     9
NelsonGon
  • 13,015
  • 7
  • 27
  • 57