Check for empty json output from phantombuster node

I have an output from the phantombuster node that returns profile information or not depending on whether or not there was a profile to scrape.

I’m trying to use the IF node to stop if the resulting json return is empty {}, but every time it returns TRUE even if the input json file is empty.

I’ve tried checking if the output data is empty (via string) or boolean.

Here’s an image of the sequence
image

Hey @rgv,

Welcome to the community :sparkling_heart:

Can you share the output you receive from the Phantombuster node? Please replace any sensitive data with dummy data.

It will also help if you can share the workflow (you can select the codes, use the keyboard shortcuts CTRL(CMD)+C and paste it here!)

1 Like

Hey @harshil1712 ,

Glad to be here :slightly_smiling_face:. Here is the coopy of the codes

{
“nodes”: [
{
“parameters”: {
“triggerTimes”: {
“item”: [
{
“hour”: 6,
“minute”: 30
},
{
“hour”: 12,
“minute”: 30
},
{
“hour”: 18,
“minute”: 30
},
{
“hour”: 0,
“minute”: 30
}
]
}
},
“name”: “Cron”,
“type”: “n8n-nodes-base.cron”,
“typeVersion”: 1,
“position”: [
-350,
200
]
},
{
“parameters”: {
“operation”: “getOutput”,
“agentId”: “8173386509139668”,
“additionalFields”: {}
},
“name”: “Phantombuster”,
“type”: “n8n-nodes-base.phantombuster”,
“typeVersion”: 1,
“position”: [
-190,
200
],
“alwaysOutputData”: false,
“credentials”: {
“phantombusterApi”: “PhantomBuster”
}
},
{
“parameters”: {
“conditions”: {
“string”: [],
“number”: [],
“boolean”: [
{
“value1”: null
}
]
}
},
“name”: “IF-Check If Results Has Email”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 1,
“position”: [
20,
200
]
}
],
“connections”: {
“Cron”: {
“main”: [
[
{
“node”: “Phantombuster”,
“type”: “main”,
“index”: 0
}
]
]
},
“Phantombuster”: {
“main”: [
[
{
“node”: “IF-Check If Results Has Email”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

Hey @rgv,

Can you also share the output of the Phantombuster node?

Currently I have an output which is not empty. This is what it looks like , It’s all sensitive information (their profile info basically)

{
“general”: {
“imgUrl”: “CLEANED”,
“fullName”: “CLEANED”,
“headline”: “CLEANED”,
“location”: “CLEANED”,
“connections”: “CLEANED”,
“profileUrl”: “CLEANED”,
“connectionsCount”: 284,
“connectionDegree”: “1er”,
“mutualConnectionsText”: “CLEANED”,
“description”: “”,
“subscribers”: “287”,
“firstName”: “CLEANED”,
“lastName”: “CLEANED”,
“vmid”: “CLEANED”,
“linkedinSalesNavigatorUrl”: “CLEANED”,
“mutualConnectionsUrl”: “CLEANED”,
“connectionsUrl”: “CLEANED”,
“countryCode”: “CLEANED”,
“userId”: “CLEANED”,
“company”: “CLEANED”,
“school”: “CLEANED”
},
“jobs”: [
{
“companyName”: “CLEANED”,
“companyUrl”: “CLEANED”,
“jobTitle”: “CLEANED”,
“dateRange”: “CLEANED”,
“location”: “CLEANED”,
“description”: “CLEANED”,
“logoUrl”: “CLEANED”
},
“timestamp”: “2021-09-23T11:56:44.431Z”
}

Otherwise when it’s empty it looks like this

[
{
}
]

Hey @rgv,

I created a small workflow that might help

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [{json:{}}];\n\nreturn [\n  {\n    json: {\n      \"general\":{\n        \"userId\": \"adfasf\"\n      }\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        970,
        250
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"general\"][\"userId\"]}}",
              "operation": "isEmpty"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1170,
        250
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The workflow uses a Function node to mock the data. The first return statement in the Function node returns an empty object, similar to what you receive. In the IF node, I am checking if the userId is present or not. Since we are returning an empty object, the userId is not present. Hence, in this case the IF node will return.

If you comment out the first return statement, and the Function node will now return a userId. When you execute the IF node, this time it will return false, since the userId is present.

I hope this helps :slight_smile:

2 Likes

Really helpful thanks :star_struck:! Makes everything much clearer for me for how to search in the json output via specific fields to see if they exist or not… also got me to understand the function node better for testing/debugging before connecting.

Thanks again!

That’s perfect! The Function node I used here was just for demonstration. You can always throw one in your workflow if you want to test things out, but their real power lies in the ability of writing custom code!

Anyways, I am glad you found it helpful. Have fun :tada:

You can also use a ternary operator in your If statement, If you set the input as Bool then click on the cog and add an expression you can do something like:

{{$json["general"] ? true : false}}

What this will do is return True if the key exists and False if it doesn’t…

From there you can configure your If as needed so toggle on the value and your true output will be for a valid option and false will be for missing.

image

3 Likes

Yes It’s clearer now. There’s a follow up question that I have as it might help tohers who are using the phantombuster node. The node output data from phantombuster is an array of the profile information, but I only want the last item in the array, separated out as a single item. See the attached image

I assume this could be done using the function node with some custom java script yes? I’m able to access the correct data in the rest of the work flow via the following parameter path : {{ $item($items.length+2).$node[“Phantombuster”].json[“details”][“mail”] }}

The expression that you have shared should work. You can return the item from whichever index you want.

1 Like

I’m having some difficulty pulling only the last item from the array and outputting it into a new array. At the moment the best I’ve been able to do is replicate the array in a new array. I must be missing something somewhere? Where as what I want is to create a new array that contains only the last entry of the output array from the Phantombuster node.

Here’s the current code from my function node

return [
{
json: {
  newArray: items.map(items => items.json)
}
}
]

My java knowledge is quite far behind me at this point :slightly_frowning_face:

You can use the item list helper to achieve that. Specifically the limit operation. Check the example below.


{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -70,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      position: 1,\n    }\n  },\n    {\n    json: {\n      position: 2,\n    }\n  }\n] \n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        180,
        300
      ]
    },
    {
      "parameters": {
        "operation": "limit",
        "keep": "lastItems"
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Item Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

That did it perfectly. Took a little bit to figure out as the instance needed to be updated, but once updated it worked like a charm.

Thanks a million!

1 Like