Misha
Misha

Reputation: 3126

Write JSON txt query to file in r

query<-'{
  "query": [
    {
      "code": "Region",
      "selection": {
        "filter": "item",
        "values": [
          "3010",
          "4020"          
        ]
      }
    },
    {
      "code": "Sex",
      "selection": {
        "filter": "item",
        "values": [
          "1",
          "2"
        ]
      }
 }
    
  ],
  "response": {
    "format": "json-stat2"
  }
}'

Now - how can I write the query string to file and retrieve it. So far I've tried writelines, save etc, but I retrieve multiple strings rather than one long string.

Upvotes: 0

Views: 34

Answers (1)

user2974951
user2974951

Reputation: 10375

The issue is that you have "\n" in your string, which forces new lines in your saved file, and then when you read it you will get multiple string. One option is to read it and then collapse it

paste0(readLines("abc.txt"),collapse="")

another is to deal away with the nuisances when writing the file, then you will get only one string

writeLines(gsub('"',"'",gsub("\n| ","",query)),"abc.txt")

Upvotes: 1

Related Questions