# Stdout to http response as JSON

**URL:** <https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566>\
**Category:** General\
**Created:** [20 August 2019 20:54 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566 "2019-08-20T20:54:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhouxf53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zhouxf53/32/11163_2.png) [@zhouxf53](https://discourse.nodered.org/u/zhouxf53)\
**Post date:** [20 August 2019 20:54 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/1 "2019-08-20T20:54:16Z")

</div>

Hi all,

Right now, I'm creating a REST API with node-red which took an **HTTP in** as the input, process the HTTP get parameters with python using **exec** , and try to send the response with JSON format.

In **exec** node, the output is sending out as stdout with Python `print` function, but I'm stuck in how to format the stdout as JSON string. Link `print("{'error': 123}")` with **template** of `{{ payload}}` then **Set Headers** and **http response** does not work in my case.

Any suggestions would be appreciated. I've the freedom to either format the `print` function from my python code or directly modifly the node-red **template**.

Edited: I figured out. The **template** of `{{ payload}}` is not necessary and it would try to escape the quotation marks. Simply `print('{"error": 123, "dfsfsfs": 456}')` and link **exec** to **http response** is good enough.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 August 2019 06:06 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/2 "2019-08-21T06:06:13Z")

</div>

I expect there is a Python library somewhere that will do that for you.

However, this seems a bit sideways. Why not just do it in Node-RED which can handle JSON natively?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [21 August 2019 07:26 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/3 "2019-08-21T07:26:38Z")

</div>

For your python code,

```auto
import json

# rest of your code

return_obj = {"error": 123, "other_value": 456}
print(json.dumps(return_obj))

```

Should handle all your cases and handle nested objects and lists too without you needing to worry about using the correct quotes in all the places.

As for the rest, sounds like this is something that can either be done fully in NR, or fully in Python depending on your approach.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 August 2019 08:40 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/4 "2019-08-21T08:40:34Z")

</div>

> [@afelix](#):
>
> For your python code

Though you might want to replace the smart quotes with standard ones 🙂

```
“
"

```

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [21 August 2019 08:51 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/5 "2019-08-21T08:51:56Z")

</div>

> [@TotallyInformation](#):
>
> the smart quotes

Oops, I missed those while typing too quickly, will edit. Thanks 😛

---

<div class="post-metadata">

**Author:** ![zhouxf53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zhouxf53/32/11163_2.png) [@zhouxf53](https://discourse.nodered.org/u/zhouxf53)\
**Post date:** [21 August 2019 20:34 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/6 "2019-08-21T20:34:10Z")

</div>

Thank you. I used exactly the same approach and solved this problem.

---

<div class="post-metadata">

**Author:** ![zhouxf53](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zhouxf53/32/11163_2.png) [@zhouxf53](https://discourse.nodered.org/u/zhouxf53)\
**Post date:** [21 August 2019 20:38 UTC](https://discourse.nodered.org/t/stdout-to-http-response-as-json/14566/7 "2019-08-21T20:38:52Z")

</div>

My python code needs to process data from database and machine learning models, I'm not very familiar with using node-red/JS to achieve the same objectives, that's why I'm only using node-red to do a simple parse HTTP query task.
