Search code examples
rjsonhttrjsonlite

Cannot deserialize instance with R POST()


I'm trying to get a queryID using the POST() funtion in R. It works well as long as I only use a simple JSON

library(httr)
library(jsonlite)

base_json <- paste('
{
  "segment" : "WHG_M"
}
')

id <- POST("url", 
           body = fromJSON(base_json), 
           encode = "json", 
           authenticate(username,password, type = "basic"))

However, when I try to incorporate further conditions, i.e.:

base_json <- paste('
{
  "segment" : "WHG_M",
  "administrativeSpatialFilter" : {
    "municipalityCodes" : [ 11000000 ]
  }
}
')

I get the following error for POST():

Cannot deserialize instance of `java.util.ArrayList` 
out of VALUE_NUMBER_INT token

with

fromJSON(base_json)

$segment
[1] "WHG_M"

$administrativeSpatialFilter
$administrativeSpatialFilter$municipalityCodes
[1] 11000000

Does anyone know how to solve the issue?


Solution

  • The exception contains 'java.util.', which clearly indicated that is is thrown by the REST-service written in Java.

    I guess this JSON { "segment" : "WHG_M", "administrativeSpatialFilter" : { "municipalityCodes" : [ 11000000 ] } }

    is transformed by you R-Client methods to

    {
      "segment" : "WHG_M",
      "administrativeSpatialFilter" : {
        "municipalityCodes" : 11000000
      }
    }
    

    which is not a list anymore and breaks the JSON-Parser on the server.

    You have to foce your JSON-Encoder to keep the JSON-list-structure, even though it has only one element.