I am unable to use array functions inside expressions in input fields while mapping data from previous node

Hi,

From the previous node i have input like this

[{
    "headers": {
        "host": "103.238.223.37:2291",
        "authorization": "YOUR_AUTHORIZATION_HEADER_VALUE",
        "content-type": "application/json",
        "content-length": "76",
        "connection": "keep-alive",
        "user-agent": "Mozilla/5.0 (compatible; Google-Apps-Script; beanserver; +https://script.google.com; id: UAEmdDd-fL_wkg6pEpZPd2o6ECH4LrJRmTg)",
        "accept-encoding": "gzip, deflate, br"
    },
    "params": {},
    "query": {},
    "body": {
        "row": 6,
        "values": ["2023-06-06T10:09:59.867Z", "testing", "delhi"]
    }
}]

and want to map the field of the next node like this

[
  { "q": 0, "a": "2023-06-06T10:09:59.867Z" },
  { "q": 1, "a": "testing" },
  { "q": 2, "a": "delhi" }
]

as i want to insert this newly created array into my database. So I am unable to modify the input array to the later one. I have tried this but not working, giving syntax error.

{{ $json.body.values.map((a, q) => ({ ques: q, ans: a })) }}

And tried this and its giving empty object [Object: null]

{{ $json.body.values.map(function(a, q){ return { ques: q, ans: a } }) }}

Please suggest.

Thanks

Hi @Bhawesh_Kumar, sounds like you could get the job done with the Item Lists and Set nodes. The Item Lists node can split out your body.values field, and the Set node can write the data into the q and a fields:

Here’s an example workflow transforming your example data:

Hope this helps :slight_smile:

Hey,

Thanks for the fast reply. It worked this way. But now I am getting new problem. I want to insert this data into a field in elastic search while creating a new document but I am getting error. Please suggest some way.

Can’t we insert array of objects here or should i go for separate fields for each question & answer like
q1: —
q2: — etc

and the second approach is working but i want to insert entire json data into single field.

Thanks

Hi @Bhawesh_Kumar, tbh I am not too sure about the exact requirements from ES when inserting a document like yours. Perhaps you want to try and insert {{ JSON.stringify($json.data) }} instead of the raw JSON?

Hey @MutedJam ,

No actually I want insert an array of objects into the elastic search and using JSON.stringify, it will be string and its working.

ie. authors field in the below example.
{
“name”: “A Regular Book”,
“tags”: [{ “name”: “free-shipping” }, { “name”: “summer-sale” }],
“authors”: [
{ “name”: “Regular author”, “age”: “40”, “country”: “USA” },
{ “name”: “John Doe”, “age”: “20”, “country”: “USA” }
]
}

If i directly map like {{$json.data}} then it gives me error as below. What can I do now? Can’t we insert array of objects from n8n?

Thanks

Hi @Bhawesh_Kumar, I don’t think this is an n8n problem but rather specific to the data you are sending and how Elasticsearch would process it.

You could simply use the HTTP Request node in order to send data to your Elasticsearch instance. It should be a simple POST request to http://elasticsearch:9200/topic-26877/_doc/ (where http://elasticsearch:9200 is your Elasticsarch instance URL, and topic-26877 is your index name).

This way you will get the raw ES behaviour and avoid any logic applied by n8n itself. However, as mentioned above, based on the error you’re seeing I suspect Elasticsearch instance would reject your raw request as well tbh, even it’s not coming from n8n. You’d probably need to use unique field names for each data type here.

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