From tibble to JSON

61 Views Asked by At

I'm facing a problem trying to convert a tibble to a JSON. It should be pretty straightforward, but looks like it's not.

This is what I've done and achieved so far:

# Original tibble
original_tibble <- tibble::tibble(
  matter = c("「Organization Module - Organization of the address book panel", 
             "「Member」 module - set the department"),
  `Problem description` = c("It is essentially the same as the above requirements", 
                            "After selecting a person, you can adjust multiple departments to which he belongs\nAfter selecting a department, you can also add members to the current department"),
  classification = c("Product demand", "product demand"),
  `review date` = c("2019-10-30T00:00:00.000Z", "2019-10-29T16:00:00.000Z")
)

# Nested tibble
nested <- original_tibble |> 
  rowwise() |> 
  nest(fields = everything())


# Convert to JSON
toJSON(list(records = nested), pretty = T)

But this is what I'm getting:

{
  "records": [
    {
      "fields": [
        {
          "matter": "「Organization Module - Organization of the address book panel",
          "Problem description": "It is essentially the same as the above requirements",
          "classification": "Product demand",
          "review date": "2019-10-30T00:00:00.000Z"
        },
        {
          "matter": "「Member」 module - set the department",
          "Problem description": "After selecting a person, you can adjust multiple departments to which he belongs\nAfter selecting a department, you can also add members to the current department",
          "classification": "product demand",
          "review date": "2019-10-29T16:00:00.000Z"
        }
      ]
    }
  ]
} 

And this is what I need:

{
  "records": [
    {
      "fields": {
        "matter": "「Organization Module - Organization of the address book panel",
        "problem description": "It is essentially the same as the above requirements",
        "classification": "Product demand",
        "Review date": "2019-10-30T00:00:00.000Z"
      }
    },
    {
      "fields": {
        "matter": "「Member」 module - set the department",
        "Problem description": "After selecting a person, you can adjust multiple departments to which he belongs\nAfter selecting a department, you can also add members to the current department",
        "classification": "product demand",
        "review date": "2019-10-29T16:00:00.000Z"
      }
    }
  ],
  "fieldKey": "name"
}

Slightly but dramatically —for the purpose— different.

1

There are 1 best solutions below

0
jrosell On BEST ANSWER

You can try this:

library(tidyverse)
library(yyjsonr)

original_tibble <- tibble::tibble(
    matter = c("「Organization Module - Organization of the address book panel", 
               "「Member」 module - set the department"),
    `Problem description` = c("It is essentially the same as the above requirements", 
                              "After selecting a person, you can adjust multiple departments to which he belongs\nAfter selecting a department, you can also add members to the current department"),
    classification = c("Product demand", "product demand"),
    `review date` = c("2019-10-30T00:00:00.000Z", "2019-10-29T16:00:00.000Z")
)

nested <- original_tibble |> 
    mutate(row = row_number()) %>% # So we nest each record
    nest(.by = row)

nested$data %>% 
    map(\(x) { list(fields = c(x)) }) %>% 
    list(records = ., fieldKey = "name") %>% 
    yyjsonr::write_json_str(opts = opts_write_json(pretty = TRUE, auto_unbox = TRUE)) %>%
    cat()
#> {
#>   "records": [
#>     {
#>       "fields": {
#>         "matter": "「Organization Module - Organization of the address book panel",
#>         "Problem description": "It is essentially the same as the above requirements",
#>         "classification": "Product demand",
#>         "review date": "2019-10-30T00:00:00.000Z"
#>       }
#>     },
#>     {
#>       "fields": {
#>         "matter": "「Member」 module - set the department",
#>         "Problem description": "After selecting a person, you can adjust multiple departments to which he belongs\nAfter selecting a department, you can also add members to the current department",
#>         "classification": "product demand",
#>         "review date": "2019-10-29T16:00:00.000Z"
#>       }
#>     }
#>   ],
#>   "fieldKey": "name"
#> }

Created on 2024-01-22 with reprex v2.1.0.9000