# How to convert "standata" to "json"?

**URL:** <https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020>\
**Category:** CmdStan\
**Created:** [July 27, 2020, 10:56am UTC](https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020 "2020-07-27T10:56:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msk98](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@msk98](https://discourse.mc-stan.org/u/msk98)\
**Post date:** [July 27, 2020, 10:56am UTC](https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020/1 "2020-07-27T10:56:50Z")

</div>

I am trying to run brms models in “cmdstanr” as,

```
custom_model = cmdstan_model("Code/custom_code.stan")
time0 = system.time(fit <- custom_model$sample(custom_standat,
                                             chains = 4,
                                             parallel_chains = 4,
                                             refresh = 1000))

```

The error is,

```
Error: No method asJSON S3 class: standata

```

How to convert standata to json?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [July 27, 2020, 11:33am UTC](https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020/2 "2020-07-27T11:33:46Z")

</div>

Can you specify a bit more what is `custom_standat` and how do you construct it?

In general, data for cmdstanr is just a list.

`fit <- custom_model$sample(list(N=5, y = c(0,1,2,3,4)),...`

---

<div class="post-metadata">

**Author:** ![msk98](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@msk98](https://discourse.mc-stan.org/u/msk98)\
**Post date:** [July 27, 2020, 11:51am UTC](https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020/3 "2020-07-27T11:51:35Z")

</div>

I have used “make\_standata(Formula, Data)” in the brms library. It returns a list[N] (S3: standata).

---

<div class="post-metadata">

**Author:** ![msk98](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@msk98](https://discourse.mc-stan.org/u/msk98)\
**Post date:** [July 27, 2020, 12:38pm UTC](https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020/4 "2020-07-27T12:38:25Z")

</div>

I have unpacked the data from the “list[N] (S3: standata)” format and packed it again to a normal “list[N]” using for loops in R. The code runs fine.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [July 27, 2020, 12:46pm UTC](https://discourse.mc-stan.org/t/how-to-convert-standata-to-json/17020/5 "2020-07-27T12:46:48Z")

</div>

Yes, was just meaning to write that. For future reference, below code does the job.

```
library(brms)

standata <- make_standata(count ~ zAge + zBase * Trt + (1|patient), 
            data = epilepsy, family = poisson())

data <- list()
for (t in names(standata)) {
  data[[t]] <- standata[[t]]
}
```
