The input says “Item 1” - so this should be an array of item 1.
But that would suggest more something like $json[0].dashboard.
But neither that nor $json.dashboard[0] give the result I am looking for.
This all feels a little hard.
I must be missing something.
When you run it do you still see the same thing? Is there also any data in the annotations or is it empty? If you can share the JSON I can take a quick look and pop something together for you.
Sorry for the delay, So looking at that data I would typically do what you tried in your first approach but if I was sending it in say an HTTP Request node I would do {{ JSON.stringify( $json.dashboard ) }}.
Typically you would only need to do [x] if the item is in an array but because it isn’t we can just call it directly. As an example the workflow below sets you data in the Code node and the HTTP request node posts the the data to webhook.site.
Oddly enough when I did just the $json.dashboard I got the output as expected but it was an object and the input I was using expected a string.
If you try the same thing with the output from the code node so you still get the missing value? I also found that it would output [object object] which was I did the stringify.
A lot of the time just using the expression is right but if you are using an object in a string you can run into some issues which isn’t unique to n8n
This part of n8n still feels very “rough”, if not to say broken.
I am still utterly confused about the behaviour.
I can make things work - but it feels awkward.
What I ended up doing was adding another Code node in front.
I guess it comes down to the input field and where it is going. So is it a string expected or json and is the input field a string or a json field.
After a few workflows you will get the hang of it and are working on improvements to how data is transformed.
You shouldn’t need a code node for it though as I showed in the example but one of the neat things with n8n is there is many ways you can do the same thing.
The way I understand it it’s just an array of objects [{},{},{}] (3 items in this example) that is getting passed through a pipeline of nodes. Which is why a JSON.stringify feels completely out of place.
The return type of JSON.stringify($json.dashboard) is a string.
$ node
Welcome to Node.js v18.14.2.
Type ".help" for more information.
> { dashboard: JSON.stringify({}) }
{ dashboard: '{}' }
…and not { dashboard: {} }
While it can be explained why it works in this case, it feels very awkward and fragile.
The approach with the additional Code node that transforms the data and then have the expression just stringify feels more verbose - but also cleaner. I guess I rather stick with that for now.
It’s not my first rodeo. Just the time with the new version of Code node.
But the the API/interface of the Code node has always been awkward.