Reputation: 92
This might have been asked and solved before, I just can't get a straightforward answer.
I got the following:
text <- 'Testing to be translated'
Which I am trying to get into JSON format like:
[{"Text": "Testing to be translated"}]
I have tried using toJSON but I could not get that structure. Additionally, I did some quick-fix:
paste0('[{"Text":"', text, '"}]')
Which would work fine; however, I have some strings with the " and ' characters in it and they would break this code.
Any input would be helpful. More context: I am using a GET request to translate text from Azure server, could not use translateR so I am creating my own function.
Upvotes: 0
Views: 667
Reputation: 57686
To create an array, pass jsonlite::toJSON
an unnamed list or vector. You should also set auto_unbox=TRUE
so that scalars aren't treated as arrays.
text <- 'Testing to be translated'
jsonlite::toJSON(list(list(Text=text)), auto_unbox=TRUE)
# [{"Text":"Testing to be translated"}]
Upvotes: 1