0

I have html as character in R and trying to convert it to JSON using jsonlite package.

   toJSON("<span>ASD</span>")

It returns <\/span> instead of </span>

["<span>ASD<\/span>"]

cat(toJSON("<span>ASD</span>")) also returns the same conversion

Ujjawal Bhandari
  • 1,333
  • 1
  • 8
  • 16
  • cat(toJSON("ASD")) is giving the same escaped version in the console. – Ujjawal Bhandari Mar 12 '22 at 22:36
  • Which package are you using for the `toJSON` function? – MrFlick Mar 12 '22 at 22:38
  • jsonlite package – Ujjawal Bhandari Mar 12 '22 at 22:38
  • 1
    Ok. It seems that escaping the forward slash is optional in JSON https://stackoverflow.com/q/1580647/2372064 and that jsonlite has decided do escape it by default. This doesn't appear to be a setting you can change. But since it's allowed by the spec normal JSON readers shouldn't have a problem correctly reading in the data as a single slash. Related github issue: https://github.com/jeroen/jsonlite/issues/255 – MrFlick Mar 12 '22 at 22:43
  • Note that `rjson::toJSON("ASD")` does not escape the slash. So different R packages have different behaviors. – MrFlick Mar 13 '22 at 00:53
  • `jsonify::to_json("ASD")` doesn't escape the `/` either. – SymbolixAU Mar 17 '22 at 04:02

0 Answers0