'Build JSON content in R according Google Cloud Pub Sub message format

In R, I want to build json content according this Google Cloud Pub Sub message format: https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage

It have to respect :

{
  "data": string,
  "attributes": {
    string: string,
    ...
  },
  "messageId": string,
  "publishTime": string,
  "orderingKey": string
}

The message built will be readed from this Python code:

def pubsub_read(data, context):
    '''This function is executed from a Cloud Pub/Sub'''
    message = base64.b64decode(data['data']).decode('utf-8')
    file_name = data['attributes']['file_name']

This following R code builds a R dataframe and converts it to json content:

library(jsonlite)
data="Hello World!"
df <- data.frame(data)
attributes <- data.frame(file_name=c('gfs_data_temp_FULL.csv'))
df$attributes <- attributes

msg <- df %>%
    toJSON(auto_unbox = TRUE, dataframe = 'columns', pretty = T) %>%
    # Pub/Sub expects a base64 encoded string
    googlePubsubR::msg_encode() %>%
    googlePubsubR::PubsubMessage()

It seems good but when I visualise it with a json editor :

enter image description here

indexes are added.

Additionally there is the message content: enter image description here

I dont'sure it respects Google Cloud Pub Sub message format...



Solution 1:[1]

Not sure why, but replacing the dataframe by a list seems to work:

library(jsonlite)

df = list(data = "Hello World")
attributes <- list(file_name=c('toto.csv'))
df$attributes <- attributes

df %>%
  toJSON(auto_unbox = TRUE, simplifyVector=TRUE, dataframe = 'columns', pretty = T)

Output:

{
  "data": "Hello World",
  "attributes": {
    "file_name": "toto.csv"
  }
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anis R.