MySQL output to IF statement

Hello,
i don’t understand working with json and items. Even with documentation :frowning: Sorry for basic question.

I have output of node MySQL like this

[
{
"Time": "2021-11-15T23:27:20.000Z",
"Price": 63556.6
},
{
"Time": "2021-11-15T23:26:20.000Z",
"Price": 63539.5
},
{
"Time": "2021-11-15T23:25:20.000Z",
"Price": 63535
},
{
"Time": "2021-11-15T23:24:20.000Z",
"Price": 63522.5
},
{
"Time": "2021-11-15T23:23:20.000Z",
"Price": 63515.2
}
]

How can I use that json in my IF node? At final I would like to compare each Price and if all of them are increasing to get phone notification (node Pushover).

My problem is that json data aren’t properly named and that’s why I can’t access them by node Set or Item Lists

Workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -150,
        240
      ]
    },
    {
      "parameters": {
        "userKey": "u83674bxjbb9vkdfg3tqt2qimww3pc",
        "message": "Ahoj",
        "priority": 0,
        "additionalFields": {}
      },
      "name": "Pushover",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        640,
        130
      ],
      "credentials": {
        "pushoverApi": {
          "id": "3",
          "name": "Pushover account"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM BTCpo1min\nWHERE Čas\nORDER BY Čas DESC -- this means highest number (most recent) first\nLIMIT 5;"
      },
      "name": "MySQL",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        150,
        240
      ],
      "alwaysOutputData": false,
      "credentials": {
        "mySql": {
          "id": "2",
          "name": "MySQL docker n8n"
        }
      }
    },
    {
      "parameters": {
        "combineOperation": "=all"
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        390,
        240
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        640,
        350
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "MySQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Pushover",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you!

might be stupid way to do it, but I guess you could use 2x Item Lists node on the mysql output
in one Item Lists sort by Time, in the other Item Lists sort by Price, if both are increasing the outputs should be identical…
than you could compare these two and decide accordingly?

1 Like

Welcome to the community @ShippeR

I put together an example with almost an identical solution to what @Bahadur suggested. To use it, create a new workflow, copy the workflow below and paste it into the new workflow.

Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -1160,
        180
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n{\n  json:  {\n\"Time\": \"2021-11-15T23:27:20.000Z\",\n\"Price\": 63556.6\n},\n},\n{\n  json: {\n\"Time\": \"2021-11-15T23:26:20.000Z\",\n\"Price\": 63539.5\n},\n},\n{\n  json: {\n\"Time\": \"2021-11-15T23:25:20.000Z\",\n\"Price\": 63535\n},\n},\n{\n  json: {\n\"Time\": \"2021-11-15T23:24:20.000Z\",\n\"Price\": 63522.5\n},\n},\n{\n  json: {\n\"Time\": \"2021-11-15T23:23:20.000Z\",\n\"Price\": 63515.2\n}\n}\n\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -900,
        180
      ]
    },
    {
      "parameters": {
        "operation": "sort",
        "sortFieldsUi": {
          "sortField": [
            {
              "fieldName": "Time",
              "order": "descending"
            }
          ]
        },
        "options": {}
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        -650,
        180
      ]
    },
    {
      "parameters": {
        "operation": "aggregateItems",
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "Price"
            }
          ]
        },
        "options": {}
      },
      "name": "Item Lists1",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        -430,
        180
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $node[\"Item Lists1\"].json[\"Price\"][\"0\"] >$node[\"Item Lists1\"].json[\"Price\"][\"1\"]  }}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        -230,
        180
      ],
      "notesInFlow": true,
      "notes": "If price increase "
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        60,
        320
      ]
    },
    {
      "parameters": {
        "additionalFields": {}
      },
      "name": "Pushover",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        80,
        -20
      ],
      "disabled": true
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Item Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Item Lists": {
      "main": [
        [
          {
            "node": "Item Lists1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Item Lists1": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Pushover",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you both for help! Example workflow helps so much :blush:

1 Like