Can't get json result

Hi, so I am trying to create a workflow where after getting the query result from my Postgres database, I need to make an if statement to know which users should get a notification and after that I need to create a node for sending through OneSignal which I am going to do it through WebHook.

My problem is after I run the query which is also scheduled, I get the response alright. As seen in the pic below

But I can’t make the if statement without getting the results in numbers so I need to transform the data so the if statement can work. I tried different methods but I can’t seem to get it done. The error is " ERROR: The json-property has to be an object!

I am new to both JS and n8n so if you have any new approaches towards this please let me know.

Hi @Lirak_Kerleshi, first of all welcome to the community :tada:

If I see this correctly, you’d push items that look like so in your line 4:

{
  json: -1
}

n8n, would however, expect an object in the json key, so something like this:

{
  json: {
    myNumber: -1
  }
}

So once you return an updated data structure this should work.

That said, isn’t days_since already a field on it’s own? So you should be able to use an IF node immediately after your Postgres node like so:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    days_since: -1\n  }\n}, {\n  json: {\n    days_since: null\n  }\n}, {\n  json: {\n    days_since: -1\n  }\n}]"
      },
      "name": "Mock PostgreSQL response",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"days_since\"]}}"
            }
          ]
        }
      },
      "name": "Days Since < 0",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Yes",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {},
      "name": "No",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        900,
        400
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Mock PostgreSQL response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock PostgreSQL response": {
      "main": [
        [
          {
            "node": "Days Since < 0",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Days Since < 0": {
      "main": [
        [
          {
            "node": "Yes",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi, thank you for your help. Yeah I tried using the IF node right after my Postgres node but I can’t get the result even after the node runs since I get it just like a query and not a result. Am I doing something wrong here because as I understand it I should get the output after I run the workflow.

And as for the object, I know the syntax is not good but I don’t understand how I can make it work. This was my latest try as I was referencing some other questions here in the forum.

Sorry for the inconvenience.

Hi @Lirak_Kerleshi, many thanks for sharing these details!

From your screenshot I believe the problem could be with your expression. I understand you don’t want to get the query but the actual result from your Postgres node?

If so, you would need an expression like {{$json["since_days"]}} which would return the value of the since_days field rather than the query. You can also pick this data from the expression editor when you execute your workflow first. After clicking the Execute Workflow button at the bottom of your canvas you should see the nodes that did run successfully turn green. Now when opening the IF node and the expression editor therein you should be able to select the input data of the IF node:

image

1 Like

Awesome and thank you.

1 Like