0

I am trying to make following JSON in R:

test_json <- '{
"PropertyValues": [{"PropertyDef": 100,
  "TypedValue": {
  "DataType": 9,
  "HasValue": false,
  "Value": null,
  "Lookup": {
  "Deleted": false,
  "DisplayValue": null,
  "Hidden": false,
  "Item": 1,
  "Version": -1
  },
  "Lookups": null,
  "DisplayValue": null,
  "SortingKey": null,
  "SerializedValue": null
  }
},
  {
  "PropertyDef": 0,
  "TypedValue": {
  "DataType": 1,
  "HasValue": false,
  "Value": "my test document",
  "Lookup": null,
  "Lookups": null,
  "DisplayValue": null,
  "SortingKey": null,
  "SerializedValue": null
  }
  }
  ],
  "Files": [
  {
  "UploadID": 1,
  "Title": null,
  "Extension": "txt",
  "Size": 27
  }
  ]
  }'

This is my try to make above JSON from list object and using jsonlite package:

create_object <- function(){
  json_query <- list(
    PropertyValues = list(
      PropertyDef = 100,
      TypedValue = list(
        DataType = 9,
        HasValue = FALSE,
        Value = NA,
        Lookup = list(
          Deleted = FALSE,
          DisplayValue = NA,
          Hidden = FALSE,
          Item = 6,
          Version = -1
        ),
        Lookups = NA,
        DisplayValue = NA,
        SortingKey = NA,
        SerializedValue = NA
      ),
      PropertyDef = 0,
      TypedValue = list(
        DataType = 1,
        HasValue = FALSE,
        Value = "090216ba8014f3be",
        Lookup = NA,
        Lookups = NA,
        DisplayValue = NA,
        SortingKey = NA,
        SerializedValue = NA
      )
    ),
    Files = list(
      UploadID = 11,
      Title = "090216ba8014f3be",
      Extension = "pdf",
      Size = 72284
    )
  )
  json_query <- toJSON(json_query, pretty = TRUE, auto_unbox = TRUE)
  json_query <- gsub("\\.1", "", json_query)
}
test_json_q <- create_object()
jsonlite::validate(test_json_q)

but this 2 JSON objects are not identical:

identical(test_json, test_json_q)

As I see it the problem is that first JSON object has [] inside {}, but I don't know how to overcome this issue.

Mislav
  • 1,533
  • 16
  • 37
  • 1
    [] indicates an array of objects in the json spec. So your PropertyValues probably wants to be inside another list (maybe?). Check the documentation , try the `asIs` argument https://www.rdocumentation.org/packages/RJSONIO/versions/1.3-0/topics/toJSON – Jack Brookes Jul 14 '18 at 19:38
  • I tried to add one more list and to add `asIs` argument from RJSONIO package but neither of that worked. – Mislav Jul 14 '18 at 19:45

1 Answers1

0

I have found the solution here: R create JSON in R.

So, the answer is:

create_object <- function(){
  json_query <- list(
    PropertyValues = I(list(
      list(PropertyDef = 100,
      TypedValue = list(
        DataType = 9,
        HasValue = FALSE,
        Value = NA,
        Lookup = list(
          Deleted = FALSE,
          DisplayValue = NA,
          Hidden = FALSE,
          Item = 1,
          Version = -1
        ),
        Lookups = NA,
        DisplayValue = NA,
        SortingKey = NA,
        SerializedValue = NA
      )),
      list(PropertyDef = 0,
      TypedValue = list(
        DataType = 1,
        HasValue = FALSE,
        Value = "090216ba8014f3be",
        Lookup = NA,
        Lookups = NA,
        DisplayValue = NA,
        SortingKey = NA,
        SerializedValue = NA
      ))
    )),
    Files = list(I(list(
      UploadID = 13,
      Title = "090216ba8014f3be",
      Extension = "pdf",
      Size = 72284
    )))
  )
  json_query <- jsonlite::toJSON(json_query, auto_unbox = TRUE, pretty = TRUE)
  json_query <- gsub("\\.1", "", json_query)
}
Mislav
  • 1,533
  • 16
  • 37