Data for SEO API is successful but the HTTP request result is null and Code node error Problem in node ‘Code1‘ ❌ No valid 'tasks[0].result' found in the input data

Hi I’m new to n8n. Trying to automate SEO blog content creation. The Data for SEO API is successfully integrated but the result in HTTP request is null. How can I resolve the null in HTTP request so that it can proceed to Code node which has an error of " Problem in node ‘Code1‘ :cross_mark: No valid ‘tasks[0].result’ found in the input data. [line 14]"

This is the javascript in the Code node "if ($json.tasks && Array.isArray($json.tasks) && $json.tasks[0] && $json.tasks[0].result) {
const results = $json.tasks[0].result;

return results.map(item => ({
json: {
keyword: item.keyword,
volume: item.search_volume,
cpc: item.cpc,
competition: item.competition
}
}));
} else {
console.log(“No valid task data found:”, JSON.stringify($json, null, 2));
throw new Error(“:cross_mark: No valid ‘tasks[0].result’ found in the input data.”);
}
"
Please help me!

Hey @bgconz hope all is well, and welcome to the community.

What is $json.tasks in the Code node?

1 Like

Hi @jabbson thanks and happy to be here in the community :beating_heart:

The $json.tasks in the Code node is for accessing the value of the tasks key in the DataForSEO API. I wanna get the actual data (like keywords, volume, CPC, etc.)

If you could upload the workflow that would help a lot.

Also, what the input to the code node looks like? I wonder if $json.tasks is even a thing, are you sure this is how you should be referencing the input?

1 Like

That’s my template and tentative workflow as I worked it one at a time. I’m changing some tools and API’s which is free to use because I didn’t have a budget for subscription yet. Recently I’m stuck at 2nd workflow because of the null and error of my Code node.

The issue appears to be with the Agent Node, not the Node code.

The variables “Title” and “Keyword” are not present. “Title” does not exist at all, and “Keyword” is written in lowercase (“keyword”) in the Node code.

Additionally, the Simple Memory is not functioning because no sessionId is defined.

Recommendation: If you do not need memory, simply remove it.

You don’t need a code node to split items out and do mapping.
Here an example.

only the Title is not defined.

2 Likes

Hi @Franz thanks for the help.

In my Google Sheet node the Title and Keyword were exist. How can I make my variables Title and Keyword to be present? Also I run your example workflow and it runs smoothly but when I tried to connect your example workflow and modify mine and tried to test the Http request node unpin and test, it gets back to the previous null and for the rest of each nodes executed successfully but no outputs.

May you share a few lines of the Google Sheets or pin the data.
The keywords in the HTTP Request node are also hardcoded and not taken from the document.
Without concrete data it’s hard to guess.

Assuming your spreadsheet has a Title and Keyword column. And the Keyword column is a list of comma separated keywords. (= Code node in example)
You can use the keywords from your spreadsheet in your http request with javascript like:
{{ $json.Keyword.split(',').map(item => item.trim()).toJsonString() }}

After that you can Merge with the data from the Spreadsheet node (=Code node in example).
When spliting up the data you can simply check the option to Include All Other Fields.

Regarding the HTTP request. I pinned the response data from keywords_data/google_ads/search_volume/live – DataForSEO API v.3

If your workflow stops after the HTTP Request code node, it might be because the results from the task[0] are empty. However, this is just a guess.

Why might this be happening? n8n workflows work by processing nodes one after the other. The workflow stops if there are no following nodes or if a node doesn’t return an item.

Depending on what you want to do, each node has an option under Settings to select “Always Output Data.” If you choose this option, the workflow will continue but with an empty item.

1 Like

The first thing that jumped out was that the code node is configured with “Run Once for All Items”, which makes $json to be not a thing, it can only be used in “Run Once for Each Item”. This is what causes the error from your original message. Is this something you have changed yourself?

1 Like

This is the sample Google Sheets that I’m working. I want the title and keywords from the document will be pass thru the HTTP Request node without a null result or how the results from the task[0] will not be empty.

I’d tried to changed it in “Run Once for Each Item” but the error still occur.

No, never mind, the code does run, even though it shows a tooltip saying it shouldn’t work. So you are still getting the error from throw? I used the pinned node from Franz’s post and it appears that the Code node processes it just fine, can you share the output of the api call to api.dataforseo.com?

1 Like

Thanks jabbson! I think you’re right that Franz’s pinned node just work fine. My bad because at his first reply he already resolved my problem, I didn’t copy and paste his pinned node to the HTTP request node in JSON field.

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