R convert data.frame to json
<p>I'm trying to convert a data.frame into json format</p>

<p>my data.frame has the following structure</p>

<pre><code>a   &lt;- rep(c("Mario", "Luigi"), each = 3)
b   &lt;- sample(34:57, size = length(a))
df  &lt;- data.frame(a,b)
&gt; df
      a  b
1 Mario 43
2 Mario 34
3 Mario 36
4 Luigi 45
5 Luigi 52
6 Luigi 35
</code></pre>

<p>What I want to create is something like this (to finally print it to a .json file)</p>

<pre><code>[
  {
    "a": "Mario",
    "b": [43, 34, 36]
  },
  {
    "a": "Luigi",
    "b": [45, 52, 35]
  }
]
</code></pre>

<p>I've tried different packages handling json format but so far failed to produce this kind of output. I usually end up with something like this</p>

<pre><code>[
  {
   "a":"Mario",
   "b":43
  },
  {
   "a":"Mario",
   "b":34
  },
  {
   "a":"Mario",
   "b":36
  },
  {
   "a":"Luigi",
   "b":45
  },
  {
   "a":"Luigi",
   "b":52
  },
  {
   "a":"Luigi",
   "b":35
  }
]
</code></pre>