# Contrib JSON node returns nothing when using jq engine

**URL:** <https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103>\
**Category:** General\
**Created:** [27 August 2020 14:27 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103 "2020-08-27T14:27:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterL](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@PeterL](https://discourse.nodered.org/u/PeterL)\
**Post date:** [27 August 2020 14:27 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/1 "2020-08-27T14:27:13Z")

</div>

Hi,

Bit of a NR newbie...I simply want to use the jq engine but it returns nothing. I've setup the contrib-json node using the example in the node description. My simple flow uses an inject not to pass JSON in msg.payload. If I run the jq command in a shell, it works perfectly. Nothing logged even if log level is set to trace. My flow is as follows:

```auto
[
    {
        "id": "e8fb13e0.4b16f",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "ebe1cdf6.44d1a",
        "type": "debug",
        "z": "e8fb13e0.4b16f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 880,
        "y": 440,
        "wires": []
    },
    {
        "id": "ab8a8d71.2802b",
        "type": "contrib-json",
        "z": "e8fb13e0.4b16f",
        "engine": "jq",
        "command": "/usr/bin/jq",
        "expr": ".foo[-1:] | {last: .[]}",
        "complete": "property",
        "prop": "payload",
        "name": "Run JQ",
        "x": 660,
        "y": 380,
        "wires": [
            [
                "ebe1cdf6.44d1a"
            ]
        ]
    },
    {
        "id": "613cc843.4e14e8",
        "type": "inject",
        "z": "e8fb13e0.4b16f",
        "name": "",
        "topic": "",
        "payload": "{\"foo\":[\"a\",\"b\",\"c\"]}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 450,
        "y": 340,
        "wires": [
            [
                "ab8a8d71.2802b"
            ]
        ]
    }
]

```

Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 August 2020 15:52 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/2 "2020-08-27T15:52:15Z")

</div>

Hi Peter,

I didnt know what jq engine is so i looked it up on the net.  
I seems that it does filtering and processing on json ?  
You should look into **jsonata** that is already included in Node-RED by default in a **Change node** by choosing expression.  
This way you dont even need to install a node that hasnt been updated in the last 5 years.

 ![Screenshot_2020-08-27 Node-RED 192 168 1 160](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b844b40954caff40f7540bee5a5fea3124dedc5.png)

More information on **jsonata** [https://jsonata.org/](https://jsonata.org/)

Example in Flow that filters all phone 'numbers' from a complicated json (at any level \*\*) and take the last one :

```auto
[{"id":"59a44afc.500bcc","type":"debug","z":"a9ccf79.74c3888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":280,"wires":[]},{"id":"2ac52666.ce1b9a","type":"inject","z":"a9ccf79.74c3888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"FirstName\":\"Fred\",\"Surname\":\"Smith\",\"Age\":28,\"Address\":{\"Street\":\"Hursley Park\",\"City\":\"Winchester\",\"Postcode\":\"SO21 2JN\"},\"Phone\":[{\"type\":\"home\",\"number\":\"0203 544 1234\"},{\"type\":\"office\",\"number\":\"01962 001234\"},{\"type\":\"office\",\"number\":\"01962 001235\"},{\"type\":\"mobile\",\"number\":\"077 7700 1234\"}],\"Email\":[{\"type\":\"office\",\"address\":[\"fred.smith@my-work.com\",\"fsmith@my-work.com\"]},{\"type\":\"home\",\"address\":[\"freddy@my-social.com\",\"frederic.smith@very-serious.com\"]}],\"Other\":{\"Over 18 ?\":true,\"Misc\":null,\"Alternative.Address\":{\"Street\":\"Brick Lane\",\"City\":\"London\",\"Postcode\":\"E1 6RF\"}}}","payloadType":"json","x":230,"y":280,"wires":[["8b51afda.ae8608"]]},{"id":"8b51afda.ae8608","type":"change","z":"a9ccf79.74c3888","name":"Filter last number","rules":[{"t":"set","p":"payload","pt":"msg","to":"[**.number][-1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":280,"wires":[["59a44afc.500bcc"]]}]

```

---

<div class="post-metadata">

**Author:** ![PeterL](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@PeterL](https://discourse.nodered.org/u/PeterL)\
**Post date:** [27 August 2020 16:39 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/3 "2020-08-27T16:39:39Z")

</div>

Thanks for the info. I have looked at jsonata, but I need to be able to convert the JSON to LDJSON and there doesn't seem to be way to do that. jq does do that easily. For example  
[  
{ "identifier": "ABC",  
"enabled":true},  
{"identifier":"XYZ",  
"enabled":true}  
]

Would need to be converted to:

{"identifier":"ABC","enabled":true}  
{"identifier":"XYZ","enabled":true}

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 August 2020 17:32 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/4 "2020-08-27T17:32:58Z")

</div>

Could something like this work ?

```auto
[{"id":"ff03a7b8.ce4bb","type":"function","z":"a9ccf79.74c3888","name":"","func":"let ldjson = \"\";\nlet arr = msg.payload;\n\narr.forEach(el => {\n ldjson += JSON.stringify(el);\n})\n\nmsg.payload = ldjson;\n//ldjson = JSON.stringify(ldjson)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":240,"wires":[["f22b4ac3.e66b8"]]},{"id":"73d52dd6.24d33c","type":"inject","z":"a9ccf79.74c3888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"identifier\":\"ABC\",\"enabled\":true},{\"identifier\":\"XYZ\",\"enabled\":true}]","payloadType":"json","x":200,"y":240,"wires":[["ff03a7b8.ce4bb"]]},{"id":"f22b4ac3.e66b8","type":"debug","z":"a9ccf79.74c3888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":240,"wires":[]}]

```

Function node:

```auto
let ldjson = "";
let arr = msg.payload;

arr.forEach(el => {
    ldjson += JSON.stringify(el);
})

msg.payload = ldjson;
//ldjson = JSON.stringify(ldjson)
return msg;

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 August 2020 18:02 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/5 "2020-08-27T18:02:30Z")

</div>

> [  
> { "identifier": "ABC",  
> "enabled":true},  
> {"identifier":"XYZ",  
> "enabled":true}  
> ]  
> Would need to be converted to:  
> {"identifier":"ABC","enabled":true}  
> {"identifier":"XYZ","enabled":true}

The input appears to be an array, but the output cannot be a single message, as that wouldn't be json.  
What should the output look like, 2 single messages, or a single string ?

---

<div class="post-metadata">

**Author:** ![PeterL](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@PeterL](https://discourse.nodered.org/u/PeterL)\
**Post date:** [1 September 2020 09:19 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/6 "2020-09-01T09:19:27Z")

</div>

Thanks all.

For line delimited JSON or JSON lines, multiple JSON objects are separated by newline characters. So whilst the input is a JSON array, the output must be the elements of the array as separate JSON objects on separate lines.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [1 September 2020 09:29 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/7 "2020-09-01T09:29:14Z")

</div>

> [@PeterL](#):
>
> the output must be the elements of the array as separate JSON objects on separate lines.

Hi Peter. A slight modification in the function node could do it.  
I kept both examples

```auto
[{"id":"4030cede.8b7108","type":"function","z":"b48b096.a2f83f8","name":"","func":"let ldjson = \"\";\nlet arr = msg.payload;\n\narr.forEach(el => {\n ldjson += JSON.stringify(el);\n})\n\nmsg.payload = ldjson;\n//ldjson = JSON.stringify(ldjson)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":280,"wires":[["ccb090d2.e67d18"]]},{"id":"9b2bc13e.72b8f8","type":"inject","z":"b48b096.a2f83f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"identifier\":\"ABC\",\"enabled\":true},{\"identifier\":\"XYZ\",\"enabled\":true}]","payloadType":"json","x":240,"y":280,"wires":[["4030cede.8b7108"]]},{"id":"ccb090d2.e67d18","type":"debug","z":"b48b096.a2f83f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":280,"wires":[]},{"id":"c10ca6b0.e68eb","type":"comment","z":"b48b096.a2f83f8","name":"Array to String ","info":"","x":560,"y":220,"wires":[]},{"id":"7928a10f.9060a","type":"function","z":"b48b096.a2f83f8","name":"","func":"\nlet arr = msg.payload;\n\narr.forEach(ldjson => {\n// send seperate msg for each element in arr\n node.send({payload: ldjson}) \n \n})\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":480,"wires":[["4f9296bc.621bd8"]]},{"id":"30c54655.8979f2","type":"inject","z":"b48b096.a2f83f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"identifier\":\"ABC\",\"enabled\":true},{\"identifier\":\"XYZ\",\"enabled\":true}]","payloadType":"json","x":260,"y":480,"wires":[["7928a10f.9060a"]]},{"id":"4f9296bc.621bd8","type":"debug","z":"b48b096.a2f83f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":480,"wires":[]},{"id":"956017e8.bb5e48","type":"comment","z":"b48b096.a2f83f8","name":"JSON objects on separate lines","info":"","x":630,"y":420,"wires":[]}]

```

ps. maybe you'll need a delay node after the function node if the msgs are being sent to fast.

---

<div class="post-metadata">

**Author:** ![PeterL](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@PeterL](https://discourse.nodered.org/u/PeterL)\
**Post date:** [1 September 2020 13:47 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/8 "2020-09-01T13:47:36Z")

</div>

Hi,

Slight modification to your script to add newline character seems to do the trick:

let ldjson = "";

payload.forEach(el =\> {  
ldjson += JSON.stringify(el)+"\r\n";  
})

msg.payload = ldjson;

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [1 September 2020 16:13 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/9 "2020-09-01T16:13:23Z")

</div>

cool .. its always trial and error in these kind of situations .. unless its a nuclear power plant 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 October 2020 16:13 UTC](https://discourse.nodered.org/t/contrib-json-node-returns-nothing-when-using-jq-engine/32103/10 "2020-10-31T16:13:40Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
