1

I had asked the same question after editing 2 times of a previous question I had posted. I am sorry for the bad usage of this website. I have flagged it for deletion and I am posting a proper new question on the same here. Please look into this.

I am basically working on a recommender system code. The output has to be converted to sequence of JSON objects. I have a matrix that has a look up table for every item ID, with the list of the closest items it is related to and the the similarity scores associated with their combinations.

Let me explain through a example.

Suppose I have a matrix In the below example, Item 1 is similar to Items 22 and 23 with similarity scores 0.8 and 0.5 respectively. And the remaining rows follow the same structure.

X1 X2 X3 X4 X5 
1 22 23  0.8 0.5
34 4 87  0.4 0.4
23 7 92  0.6 0.5

I want a JSON structure for every item (every X1 for every row) along with the recommended items and the similarity scores for each combination as a separate JSON entity and this being done in sequence. I don't want an entire JSON object containing these individual ones. Assume there is one more entity called "coid" that will be given as input to the code. I assume it is XYZ and it is same for all the rows.

{ "_id" : { "coid" : "XYZ", "iid" : "1"}, "items" : [ { "item" : "22", "score" : 0.8},{ "item": "23", "score" : 0.5}] }
{ "_id" : { "coid" : "XYZ", "iid" : "34"},"items" : [ { "item" : "4", "score" : 0.4},{ "item": "87", "score" : 0.4}] }
{ "_id" : { "coid" : "XYZ", "iid" : "23"},"items" : [ { "item" : "7", "score" : 0.6},{ "item": "92", "score" : 0.5}] }

As in the above, each entity is a valid JSON structure/object but they are not put together into a separate JSON object as a whole.

I appreciate all the help done for the previous question but somehow I feel this new alteration I have here is not related to them because in the end, if you do a toJSON(some entity), then it converts the entire thing to one JSON object. I don't want that. I want individual ones like these to be written to a file.

I am very sorry for my ignorance and inconvenience. Please help. Thanks.

user2878729
  • 61
  • 1
  • 5

1 Answers1

1
library(rjson)
## Your matrix
mat <- matrix(c(1,34,23,
               22, 4, 7,
               23,87,92,
               0.8, 0.4, 0.6,
               0.5, 0.4, 0.5), byrow=FALSE, nrow=3)

I use a function (not very interesting name makejson) that takes a row of the matrix and returns a JSON object. It makes two list objects, _id and items, and combines them to a JSON object

makejson <- function(x, coid="ABC") {
    `_id` <- list(coid = coid, iid=x[1])
    nitem <- (length(x) - 1) / 2 # Number of items
    items <- list()
    for(i in seq(1, nitem)) {
        items[[i]] <- list(item = x[i + 1], score = x[i + 1 + nitem])
    }
    toJSON(list(`_id`=`_id`, items=items))
}

Then using apply (or a for loop) I use the function for each row of the matrix.

res <- apply(mat, 1, makejson, coid="XYZ")
cat(res, sep = "\n")
## {"_id":{"coid":"XYZ","iid":1},"items":[{"item":22,"score":0.8},{"item":23,"score":0.5}]}
## {"_id":{"coid":"XYZ","iid":34},"items":[{"item":4,"score":0.4},{"item":87,"score":0.4}]}
## {"_id":{"coid":"XYZ","iid":23},"items":[{"item":7,"score":0.6},{"item":92,"score":0.5}]}

The result can be saved to a file with cat by specifying the file argument.

## cat(res, sep="\n", file="out.json")

There is a small difference in your output and mine, the numbers are in quotes ("). If you want to have it like that, mat has to be character.

## mat <- matrix(as.character(c(1,34,23, ...

Hope it helps,

alex

alko989
  • 7,688
  • 5
  • 39
  • 62
  • Yeah. It helps but I could not get the JSON objects, one in each line. The elements of each JSON object come in different lines themselves. It works for the 3 row-matrix properly, but not the big matrix I have in my code. Any suggestions on how to force each JSON object to be in the same line, irrespective of the number of elements each may have? Thanks in advance. – user2878729 Jul 26 '14 at 13:43
  • Maybe some of the structure of your real data is not in the example. The idea of my answer is that for each line of data, one json object is created (using `apply`). I think the structure of your real data is not the same with the example data. My advice: take a row of your real data, think how the json object for this row has to look like and change the function in my answer to accommodate that. – alko989 Jul 28 '14 at 16:14