Nimrookie
Nimrookie

Reputation: 79

read write json with jsonlite in R

the task is to load a json, manipulate some data and write back. The first step is to just read and write the json file with identical out- and input, but this is not the case. Here a testfile which boils down the problem:

{
  "hourly": [
      {
        "Temperatur": {
          "Datum": ["2021-09-24 21:00:00", "2021-09-24 22:00:00"],
          "Vorhersage": [14.82, 14.83],
          "Referenz": [16.6, 16.2],
          "DWD_Name": ["Elpersbüttel", "Elpersbüttel"],
          "Export": [17.2, 16.71],
          "color": ["rgb(0,128,0)", "rgb(0,128,0)"],
          "Status": ["ok", "ok"],
          "min": [13.05, 12.81],
          "max": [17, 17]
        }},
      {
        "Temperatur2": {
          "Datum": ["2021-09-24 21:00:00", "2021-09-24 22:00:00"],
          "Vorhersage": [13.82, 14.83],
          "Referenz": [16.6, 16.2],
          "DWD_Name": ["Elpersbüttel", "Elpersbüttel"],
          "Export": [17.2, 16.71],
          "color": ["rgb(0,128,0)", "rgb(0,128,0)"],
          "Status": ["ok", "ok"],
          "min": [13.05, 12.81],
          "max": [17, 18]
        }
      }
    ]
}

However, jsonlite alters the data structure:

library(jsonlite)
id = "test3" # my testfile
setwd("/path/")
myjs = fromJSON(paste0(id,".json"),simplifyVector = T) 
write_json(myjs, paste0(id,"_.json"), pretty = T) # output file 'test3_.json'

Here the result I get:

{
  "hourly": [
    {
      "Temperatur": {
        "Datum": ["2021-09-24 21:00:00", "2021-09-24 22:00:00"],
        "Vorhersage": [14.82, 14.83],
        "Referenz": [16.6, 16.2],
        "DWD_Name": ["Elpersbüttel", "Elpersbüttel"],
        "Export": [17.2, 16.71],
        "color": ["rgb(0,128,0)", "rgb(0,128,0)"],
        "Status": ["ok", "ok"],
        "min": [13.05, 12.81],
        "max": [17, 17]
      },
      "Temperatur2": {
        "Datum": {},
        "Vorhersage": {},
        "Referenz": {},
        "DWD_Name": {},
        "Export": {},
        "color": {},
        "Status": {},
        "min": {},
        "max": {}
      }
    },
    {
      "Temperatur": {
        "Datum": {},
        "Vorhersage": {},
        "Referenz": {},
        "DWD_Name": {},
        "Export": {},
        "color": {},
        "Status": {},
        "min": {},
        "max": {}
      },
      "Temperatur2": {
        "Datum": ["2021-09-24 21:00:00", "2021-09-24 22:00:00"],
        "Vorhersage": [13.82, 14.83],
        "Referenz": [16.6, 16.2],
        "DWD_Name": ["Elpersbüttel", "Elpersbüttel"],
        "Export": [17.2, 16.71],
        "color": ["rgb(0,128,0)", "rgb(0,128,0)"],
        "Status": ["ok", "ok"],
        "min": [13.05, 12.81],
        "max": [17, 18]
      }
    }
  ]
}

if simplifyVector = F, this results in nested arrays.

{
  "hourly": [
    {
      "Temperatur": {
        "Datum": [
          ["2021-09-24 21:00:00"],
          ["2021-09-24 22:00:00"]
        ],
        "Vorhersage": [
          [14.82],
          [14.83]
        ],
        "Referenz": [
          [16.6],
          [16.2]
        ],
        "DWD_Name": [
          ["Elpersbüttel"],
          ["Elpersbüttel"]
        ],
        "Export": [
          [17.2],
          [16.71]
        ],
        "color": [
          ["rgb(0,128,0)"],
          ["rgb(0,128,0)"]
        ],
        "Status": [
          ["ok"],
          ["ok"]
        ],
        "min": [
          [13.05],
          [12.81]
        ],
        "max": [
          [17],
          [17]
        ]
      }
    },
    {
      "Temperatur2": {
        "Datum": [
          ["2021-09-24 21:00:00"],
          ["2021-09-24 22:00:00"]
        ],
        "Vorhersage": [
          [13.82],
          [14.83]
        ],
        "Referenz": [
          [16.6],
          [16.2]
        ],
        "DWD_Name": [
          ["Elpersbüttel"],
          ["Elpersbüttel"]
        ],
        "Export": [
          [17.2],
          [16.71]
        ],
        "color": [
          ["rgb(0,128,0)"],
          ["rgb(0,128,0)"]
        ],
        "Status": [
          ["ok"],
          ["ok"]
        ],
        "min": [
          [13.05],
          [12.81]
        ],
        "max": [
          [17],
          [18]
        ]
      }
    }
  ]
}

In both cases, the underling problem is that fromJSON generates a list object, which does not represent the data structure of the input correctly... or write_json (tojson) interprets it in a wrong way. A workaround could be to use simplifyVector = F, reload and fix the arrays afterwards..

Upvotes: 2

Views: 1358

Answers (1)

Nimrookie
Nimrookie

Reputation: 79

The option simplifyDataFrame = F solved the issue.

Upvotes: 1

Related Questions