Skip to content

toJSON question

3 messages · Santosh Srinivas, Duncan Temple Lang, Dieter Menne

#
Hello,

I am trying to use RJSONIO

I have:
x <- c(0,4,8,9)
y <- c(3,8,5,13)
z <- cbind(x,y)

Any idea how to convert z into the JSON format below?

I want to get the following JSON output to put into a php file.
 [[0, 3], [4, 8], [8, 5], [9, 13]]

Thank you.
#
On 12/11/10 8:00 AM, Santosh Srinivas wrote:
The toJSON() function is the basic mechanism.
In this case, z has names on the columns.
Remove these
  colnames(z) = NULL

Then toJSON(z) gives you want you want.

If you want to remove the new line (\n) characters,
use gsub().

  gsub("\\\n", "", toJSON(z))


  D.
#
Santosh Srinivas wrote:
I have not tried RJSONIO, but with package rjson you could use:
[1] "{\"x\":[0,4,8,9],\"y\":[3,8,5,13]}"
[1] "{\"V1\":[0,3],\"V2\":[4,8],\"V3\":[8,5],\"V4\":[9,13]}"

Dieter