1

I have a problem with rawToChar() and write_json() where my result is a .json file that includes \n and \ and cannot be viewed in a "pretty" manner using notebook+. In addition I can't unfold or Uncollapse the result.

My working sequence is:

request <- httr::GET(
   url = "https://myurl",
   httr::content_type("application/json")
 )

existing_distribution.json = jsonlite::prettify(rawToChar(request$content))

write_json(existing_distribution.json, "distribution.json"), pretty = TRUE)

When I open distribution.json with notepad+ I get a single line ...

["{\n    \"ConfigurationStatus\": \"NOT_DEFINED\",\n    \"economicStatus ... blahblah \n}\n"]

Any ideas?

SteveG
  • 99
  • 1
  • 11
  • This is hard to reproduce if we do not know the url. Why is `rawToChar` needed? – danlooo Apr 26 '22 at 08:14
  • Well the looking at the url response: req$content = "7b 22 73 74 61 74 75 etc" prettify(rawToChar(req$content)) gives something readable The url is 'internal' so that's difficult – SteveG Apr 26 '22 at 09:38
  • If I take a sample json from the top of https://json.org/example.html it's perfectly readable in atom or notepad++ json_test = read_file("../output/test.json") gives me a character string that starts "{\r\n \"glossary\": {\r\n If I then do write_json(json_test, paste0("../output/test2.json"), pretty = TRUE) I find that reading test2.json in notepad++ gives ["{\r\n \"glossary\": {\r\n \"title\": \"example glossary\",\r\n So even removing rawToChar() seems to give a problem. Or am I expecting too much? – SteveG Apr 26 '22 at 09:56

1 Answers1

1

Let there be a file example.json mocking your API containing

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

This is how to read the file as a raw sequence of bytes, parses it and saves it as prettified json:

library(jsonlite)
library(readr)

read_file_raw("example.json") %>%
  rawToChar() %>%
  parse_json() %>%
  write_json("out.json", pretty = TRUE)

Resulting in file out.json containing:

{
  "glossary": {
    "title": ["example glossary"],
    "GlossDiv": {
      "title": ["S"],
      "GlossList": {
        "GlossEntry": {
          "ID": ["SGML"],
          "SortAs": ["SGML"],
          "GlossTerm": ["Standard Generalized Markup Language"],
          "Acronym": ["SGML"],
          "Abbrev": ["ISO 8879:1986"],
          "GlossDef": {
            "para": ["A meta-markup language, used to create markup languages such as DocBook."],
            "GlossSeeAlso": [
              ["GML"],
              ["XML"]
            ]
          },
          "GlossSee": ["markup"]
        }
      }
    }
  }
}
danlooo
  • 10,067
  • 2
  • 8
  • 22