# How should the outputting/conversion of JSON be separated?

**URL:** https://discourse.slimframework.com/t/how-should-the-outputting-conversion-of-json-be-separated/4686
**Category:** Questions
**Created:** [March 6, 2021, 6:23pm UTC](https://discourse.slimframework.com/t/how-should-the-outputting-conversion-of-json-be-separated/4686 "2021-03-06T18:23:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![FatboySlim](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@FatboySlim](https://discourse.slimframework.com/u/FatboySlim)
#### Post date: [March 6, 2021, 6:23pm UTC](https://discourse.slimframework.com/t/how-should-the-outputting-conversion-of-json-be-separated/4686/1 "2021-03-06T18:23:24Z")

</div>

Hi,

In [Odan’s ebook](https://odan.github.io/2019/11/05/slim4-tutorial.html#writing-json-to-the-response) (which I did buy) it says:

> In this example, we create a “barrier” between source data and output, so that schema changes do not affect the clients. For the sake of simplicity, this is done using the same method. In reality, you would separate the input data mapping and output JSON conversion into separate parts of your application.

This is in reference to the UserCreateAction class.

I’m intending incorporating a front end framework, Angular perhaps, in my app. So how should the outputting/conversion of JSON be separated? Do you have an example you can show me?

Thank you,

FBS

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [March 6, 2021, 10:18pm UTC](https://discourse.slimframework.com/t/how-should-the-outputting-conversion-of-json-be-separated/4686/2 "2021-03-06T22:18:08Z")

</div>

Hi!

According to [ADR](https://github.com/pmjones/adr/blob/master/ADR.md#view-versus-responder) the Action should not be responsible for transforming the Domain result into proper structure for the (JSON) response. The _Responder_ is entirely in charge of setting headers, setting the body content, picking content types, rendering templates, and so on. Note that a _Responder_ may incorporate a _Template View_ , _Transform View_ , or any other kind of body content building system. In trivial cases, it may be reasonable to collect different sets of _Responder_ logic, e.g., corresponding to the presentation logic for different _Actions_, into a single class as well.  
In more complex scenarios a particular Responder would be a better choice as I mentioned in the tutorial.

In case you want to separate the output conversion (transformation), you could create a specific Responder class, e.g. `UserCreateResponder`. Then the Action should invoke this responder and return the result from it as Response object.

To transform the Domain result within the Resonder you have multiple possibilities, like manual mapping or [league/fractal](https://fractal.thephpleague.com/) or [selective/transformer](https://github.com/selective-php/transformer) or maybe [laminas/laminas-hydrator](https://docs.laminas.dev/laminas-hydrator/). It really depends on your specific use case.

---

<div class="post-metadata">

### Author: ![FatboySlim](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@FatboySlim](https://discourse.slimframework.com/u/FatboySlim)
#### Post date: [March 6, 2021, 10:27pm UTC](https://discourse.slimframework.com/t/how-should-the-outputting-conversion-of-json-be-separated/4686/3 "2021-03-06T22:27:29Z")

</div>

Hi Odan,

That makes a lot of sense. I’ll give the `UserCreateResponder` class some thought and see if I can get somewhere with it.

I have a specific idea for a project in mind so I’ll probably be asking lots more questions 😉

Thank you

---

<div class="post-metadata">

### Author: ![FatboySlim](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@FatboySlim](https://discourse.slimframework.com/u/FatboySlim)
#### Post date: [March 7, 2021, 12:23am UTC](https://discourse.slimframework.com/t/how-should-the-outputting-conversion-of-json-be-separated/4686/4 "2021-03-07T00:23:59Z")

</div>

Hi,

I added `ResponseFactoryInterface` and `RouteParserInterface` to the container and used the `Responder` in your repo. Invoked the responder in `UserCreateAction` and that did the trick. I don’t need a custom `UserCreateResponder` just yet.

Thank you!
