Going mad - how to use expressions within expression?

Hi all, I’m pulling all the hairs out of my head of frustration, I just cannot seem to get expressions within expressions to work.

I managed to get this issue almost resolved: Lost with http query, “cannot creat property accept” and arrays - Questions - n8n
The last step is… hard to explain.

I need this to get into a http request node:


for it able to work, I need to add extra brackets (no idea why, but apparently it makes it a json object:

but that doesnt work, it somehow sees the first brackets and interacts with the “rawtext”
argh!!
is there any way I can get around this? I need to be able to feed expressions within this json object. Please help me!

Hi @bees8

Hope you arent bald yet.
At a first glance you should be able to remove the {{ }} from arround the $json[] expressions.

2 Likes

hey bram, pretty bald, but I’ve got some hairs left to pull though!
Yes, when I do that all fine, and my intermittent solution was the following:

  1. put all expressions in one set node (so far so good!)


2. then create a json object (no idea if thats the name), by putting this expression into the next set node between the curly brackets. Which is basically the same as doing it directly at step 1, and thus doesnt work.
image

The result I am trying to obtain:


(in this example, I have simply copied the json on the left, so I haven’t used any expression)

I feel like there is a super easy solution for this!

Save my remaining hairs please, lol…

Thanks!

this also doesnt work

@bees8 I am very sorry but I am not sure I follow. Can you please post a simple workflow example of what you want to achieve and the how exactly the data in a certain parameter should look like. I can then have a look.

Hi @jan, Thanks for your reply.

I am having a hard time explaining this issue, so I tried my best setting up the example workflow:

the difference is in the json.
This works, but is without the expressions:
image

And this is with expressions, but it does not work:
image

Hope it makes more sense now.
Thanks again.

I did just run your example and did set as destination webhook.site. The node did execute without an error and the data also arrived fine:

I would have expected either the node to error or the data to be messed up. As neither of that happened I am sadly still lost.

Or is it possible that you want to stringify the data? So change the expression from:

{{ $json["metadata"] }}

to

{{ JSON.stringify($json["metadata"]) }}
1 Like

I do this yesterday, with help from Tom in general channel

Tom said : …sems to work. Expression-ception
:smiley:
I don’t know if that help. Sorry if not.

1 Like

@jan thanks for looking into it , no definitely not looking to stringify the data

Let’s try it this way, all I need to make it work is that the {{json[“name”]}} doesnt get negated by the first opening curly brackets, like below:

I have no idea what is happening here haha, sorry :slight_smile:

Ah, in this case, you can do something like this:

{{ {"name": $json["name"], "creator": $json["creator"] } }}
1 Like

I can’t do better :joy: :joy: :joy: :joy: :joy:

2 Likes

this finally works, thanks @jan ! I’m almost there!
I managed to get almost all the expression included, except for one.
it is an array, and one that I want to be optional, that’s why I have formatted the expression as the following:

"verAttributes": ", "array": [{"key1":"value1","key2":"value2"}]"

As you can see, this doesn’t really work out. ( for clarity, I have added the second example to show that the entire expression works fine without the verAttributes expression.

My thought was that it is probably parsed as a string, so instead of

, "array": [{"key1":"value1","key2":"value2"}]

if will most likely show up as:
, \"array\": [{\"key1\":\"value1\",\"key2\":\"value2\"}]
which is of course not a valid json format

@jan is there an opposite of your json.stringify function? Maybe that will work?

I have also tried parsing only key1 and value1, but that will not work because if they do not get parsed (because optional), I’m stuck with invalid json.

Thanks!

That’s way above my head lol.

1 Like

Theoretically should you be able to set it exactly the same way as the others. If it exists it will set it, if not then undefined will be returned and should so not show up.

Here an example:

1 Like

oh that is so cool!
Learnt a lot from this thread, thanks @jan !

In the end, I settled with a compromise though. It was great that undefined doesnt show up and in the above case it would be perfect, though late last night I found out my array wasn’t right, it actually has two key/value sets of which one is a variable, and the other not. So because one of them is fixed, it will always output an array. Anyways, all details, potato potato.

In the end the most important thing is that it is working now splendidly!
Thanks so much everyone! I can finally get some sleep :slight_smile:

**Lessons learnt: **
1. Don’t put expressions between curly brackets if they are already between curly brackets.
2. don’t put expressions between " these apostrophes "
3. if the value of a key/value pair is an expression and it is not valid, the key also will not show up, ensuring the json still stays valid

3 Likes

Hi @bees8

Nice summary!

Ps. Could you tell me what you did not understand on my first response as this is the same as the marked solution? Want to improve my answers so less people get bald. :upside_down_face:

3 Likes

Hey @BramKn - absolutely!
If you look at my first example and the lessons learnt, it was basically point 2. Your suggestions was entirely correct, though it needed some contect (doesnt work within apostrophes). @jan’s reply helped me because I decided to follow the example directly (I pasted it under my line, and it allowed me to check character for character). Thanks for your input!!

2 Likes