String to Json and remap item

Hi,

I try to adapt two legacy Function node into one Code node, but I start with javascript and I can’t readjust.

image

convert_stdout_to_json

items = JSON.parse(item.stdout);
return items

remap_item

return items[0].json.map(item => {
  return {
    json: item
  }
});

But i don’t know how to do with the new $input.all()
Have you got an idea ?

thanks

Hi @Jeremy_Bepoix, I suppose this depends a bit on how your stdout looks like.

The new Code node would require a valid n8n data structure, while the older Function/Function Item nodes were more lenient about this. The basic logic should still work though once you add the item structure.

Here is a quick example of a workflow similar to the one on your screenshot using the code node. I tried this in a Linux docker container, so if you’re using Windows you might need to use a different command on the Execute Workflow node to return valid JSON.

hum, okay.
So i can really concat convert_stdout_to_json and remap item in one Code node ?

note : what i’v got from ldap_query_cmd (a string)

is working with ‘old’ method, but i wan to minimize code node and understand to update code with the new Code node.

regards,

Yes, you can also combine the function of both nodes in a single node. For example like so:

Result:

Here I am first applying the magic of JSON.parse() to the stdout field of the first input item. $input.first() would reference the incoming JSON/table data of the first item (listed here), .json would reference an item’s JSON data (the kind of data you see in the table or JSON view of n8n) and .stdout finally points to the stdout field.

Last thing I do is use .map() to move each of the items of our array coming from stdout under a json key as per n8n’s data structure.

Hope this makes sense and helps!

1 Like

ha…yes of course,
This is where I got confused with the old method wth keeping items[0] instead $input.first().
I see it more clearly

thank you for your time!

1 Like

You are most welcome, glad to hear I could help :slight_smile:

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