0

I want to convert a data table in R to a set of json objects: Suppose my datatable looks like this:

name           group    age (y) height (cm) weight (kg) score
[1,] "Doe, John"    "Red"    "24"    "182"       "74.8"      NA   
[2,] "Doe, Jane"    "Green"  "30"    "170"       "70.1"      "500"
[3,] "Smith, Joan"  "Yellow" "41"    "169"       "60"        NA   
[4,] "Brown, Sam"   "Green"  "22"    "183"       "75"        "865"
[5,] "Jones, Larry" "Green"  "31"    "178"       "83.9"      "221"
[6,] "Murray, Seth" "Red"    "35"    "172"       "76.2"      "413"
[7,] "Doe, Jane"    "Yellow" "22"    "164"       "68"        "902"

I want it as follows:

[{"name":"Doe, John","group":"Red","age (y)":24,"height (cm)":182,"weight (kg)":74.8,"score":null},
{"name":"Doe, Jane","group":"Green","age (y)":30,"height (cm)":170,"weight (kg)":70.1,"score":500},
{"name":"Smith, Joan","group":"Yellow","age (y)":41,"height (cm)":169,"weight (kg)":60,"score":null},
{"name":"Brown, Sam","group":"Green","age (y)":22,"height (cm)":183,"weight (kg)":75,"score":865},
{"name":"Jones, Larry","group":"Green","age (y)":31,"height (cm)":178,"weight (kg)":83.9,"score":221},
{"name":"Murray, Seth","group":"Red","age (y)":35,"height (cm)":172,"weight (kg)":76.2,"score":413},
{"name":"Doe, Jane","group":"Yellow","age (y)":22,"height (cm)":164,"weight (kg)":68,"score":902}]

Can anybody help me with this?

2 Answers 2

1

Are you sure that's data.table output? The [1,] looks funny to me. I thought data.table usually did 1:. That looks more like matrix output.

But assuming it is a data.table like

dd<-data.table(
    name = c("Doe, John", "Doe, Jane", "Smith, Joan", "Brown, Sam",
        "Jones, Larry", "Murray, Seth", "Doe, Jane"), 
    group = c("Red", "Green", "Yellow", "Green", "Green", "Red", "Yellow"), 
    "age (y)" = c(24L, 30L, 41L, 22L, 31L, 35L, 22L),
    "height (cm)" = c(182L, 170L, 169L, 183L, 178L, 172L, 164L), 
    "weight (kg)" = c(74.8, 70.1, 60, 75, 83.9, 76.2, 68), 
    score = c(NA, 500L, NA, 865L, 221L, 413L, 902L)
)

then you can run

cat( toJSON( unname(split(dd, 1:nrow(dd))) ) )

to get your desired output.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry my bad. You are right!! I posted a dataframe I guess.Your answer works!! Thanks a ton!!! :)
Hey I see that the values in json objects are enclosed in square brackets like this: "name": [ "Doe, Jane" ] Is it possible to get them without the square brackets?
@RabiyaAshruff That doesn't happen for me when i run the code above (using R version 3.1.0 and rjson_0.2.14 and data.table_1.9.2). I get output that looks like what you desire. Can you make a reproducible example?
0

You can try tp use fromJSON or toJSON on the jsonlite package.

library(jsonlite)

iris2 <- fromJSON(myjson)

myjson <- toJSON(iris, pretty=TRUE)

hope it helps.

1 Comment

Hi I had actually tried that before. But it doesn't give the desired output. It just produces a single json object with all values separated by comma with no field names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.