darkage
darkage

Reputation: 857

Returning a nested JSON in R

I want to return an output similar to the below in R

"outputFiles": [
    {
      "dealType" : "Scanback",
      "claimNumer" : "1234",
      "dealDescription" : "deal1",
      "requestOutputFileName" : "<filename>",
      "filePath" : "c:/processed/files/<FileName>"
    },
    {
      "dealType" : "Rebate",
      "claimNumer" : "1234",
      "dealDescription" : "deal2",
      "requestOutputFileName" : "<filename>",
      "filePath" : "c:/processed/files/<FileName>"
    }
  ]

I have "dealType" , "claimNumber", "dealDescription", "requestOutputFileName" and "filePath" as vectors.

e.g.,

> dealType
[1] "scanback" "rebate" 

I am not able to get it in the above structure.

How can this be achieved using R?

Upvotes: 0

Views: 27

Answers (2)

Jrm_FRL
Jrm_FRL

Reputation: 1423

Doesn't toJSON() from jsonlite simply work, if you combine your vectors into a dataframe ?

toJSON(data.frame(dealtype, ClaimNumer...), 
       pretty = TRUE)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389205

Would it work if you put the vectors in a dataframe and then use toJSON to get it in JSON format.

dealType <- c("scanback", "rebate")
claimNumer <- c(1234, 1234)
df <- data.frame(dealType, claimNumer)

jsonlite::toJSON(df)
#[{"dealType":"scanback","claimNumer":1234},{"dealType":"rebate","claimNumer":1234}]

Upvotes: 1

Related Questions