title: Converting a matrix/dataframe in R to JSON object with some constraints
body: <p>REDITED:</p>
  
  <p>I have to convert a matrix in R to a JSON object with some structure in it. I am using the rjson package. Through an example, let me illustrate what I want. My specific case is the output of a recommender system code in R where the X2 X3 are the 2 closest items to a particular item X1. Also, X4,X5 are the scores of similarity associated with (X1,X2) and (X1,X3) for that row.  I want all the recommended items for every item as JSON objects and every item along with its recommended JSON object- items as a bigger JSON objects.The scores should also be incorporated into the JSON structure.</p>
  
  <p>Let me explain through a example.</p>
  
  <p>Suppose I have a matrix</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>{ "_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}] }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I am very sorry for my ignorance and inconvenience. Please help.
  Thanks.</p>