Need help to do API post to customfield that is object

Hello Team,
I am new to automation and facing an issue. When i do the API Post and set Parameter name and value it tries to send it as an array but has to be an object. So basically it is not sending anything.

I’ve tried everything I could find on help and the web but still nothing. Can someone help me?

In this case you would have to set an expression like this:

{{ { "R5tFe...": $json["someKey"] } }}

Here an example workflow:

Dear Jan,
I am finally getting some understanding of the structure but still doesn’t work.

In the expression, I have everything in red

The N8N still not showing anything

Looking on the API guide it stated that customField has to be an object, but my knowledge in API is so vague that the issues about value being send in the wrong format might be wrong.

Hi @kgurinov ,
in Jans code $json[“someKey”] is a special n8n expression. It meens: Get the value from attribute “someKey” of the node before.

This doc helps (me) a lot:
https://docs.n8n.io/nodes/expressions.html

If no node before HTTP Request $json[“someKey”] is undefined. All is red.
You can replace it with plane text for testing, like “value” >>

{ "R5tFe...": "value" }

Additional point:
If the object “R5tFe…” need a string and you want to use $json, you have to write:

{ "R5tFe...": "{{$json['someKey']}}" 

The value need additional " surrounded - from $json you don’t get it.

In {{}}-Brackets you write JS-Code :wink:

An example:

Thx @pemontto to show me the “error” in Jan’s code :sweat_smile:
But after some testing, I learned by Jan writing, it automatically creates an object and converts $json[“someKey”] to a string. The " not needet. Thanks @Jan.

Use the double curly braces {{ like a template. In your case to make the JSON

{"customfield": "value fo someKey"}

You would make the expression

{"customfield": {{ $json['someKey'] }} }

You may need inverted commas around the value

{"customfield": "{{ $json['someKey'] }}" }
1 Like

Guys you are the best, this did the trick, spent a week digging myself but instead needed to post it here :slight_smile:

and the result

Thank you very very much guys :slight_smile:

Just to make it clear. There is no error in my code. It really depends on what you need.

The expression I did use ({{ { "R5tFe...": $json["someKey"] } }}) will send the data as a proper object and you will end up with data like this:

{
  "customField": {
    "R5tFEwtnpmpuFTmFH32F": "someValue"
  }
}

If you use the expression { "R5tFEwtnpmpuFTmFH32F": "{{$json["someKey"]}}" } then the data will be sent as a string and it will end up like this:

{
  "customField": "{ \"R5tFEwtnpmpuFTmFH32F\": \"someValue\" }",
}

Almost perfect, now when i try to map the fields from the webhook to HTTP API POST getting an error

{{ { 
	"HKOVHm760DWQ3EzImlY4":"{{$node["Webhook from Leadbyte"].json["body"]["lifeinsurance"]}}","VeCp6ErrTzSoOPE0HDYm":"{{$node["Webhook from Leadbyte"].json["body"]["age"]}}","UjIg9lVRbeG4bbYrIhK4":"{{$node["Webhook from Leadbyte"].json["body"]["how_much_the_coverage"]}}","YXPZlOutLKD9E8dZrdPo":"{{$node["Webhook from Leadbyte"].json["body"]["benefeciary"]}}","3178iL062C3HZOV8qmNT":"{{$node["Webhook from Leadbyte"].json["body"]["benefeciary_name"]}}","R5tFEwtnpmpuFTmFH32F":"{{$node["Webhook from Leadbyte"].json["body"]["hobby"]}}" 
	} }}

what am I doing wrong ?

You are using using an expression in an expression:

Wrong:

{{ { "key": "{{ $node['nodeName'].json.propertyName" } }}

Correct:

{{ { "key": $node['nodeName'].json.propertyName } }}
1 Like

as usual, @jan thank you for your help.

Dear @jan, what if I have both key and value from a node?

{{ {"{{$node["Function to collect floating customfiled ID"].json["haveInsurance"]}}":$node["Webhook from Leadbyte"].json["body"]["lifeinsurance"]} }}

getting

[invalid (Expression is not valid: Unexpected string)]":$node["Webhook from Leadbyte"].json["body"]["lifeinsurance"]} }}

Hi @kgurinov,
if you need booth, then use the Set-Node with dot notation or a function: