0

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?

thmschk
  • 614
  • 3
  • 16

1 Answers1

0

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.

alfonx
  • 6,936
  • 2
  • 49
  • 58
  • Thx, alfonx. If anyone knows how to keep JSON-list-Structure, help would be highly appreciated. The help of POST() says "to preserve a length 1 vector as a vector, wrap in I()". However, I have no idea which expression I have to wrap in I(). – thmschk Jun 25 '19 at 13:24