Simplify JSON response to object

Hi,
I am trying to simplify a JSON response from:

[
[
{
"seance_length": 3900,
},
{
"seance_length": 3000,
},
],
[
{
"seance_length": 3900,
},
{
"seance_length": 1800,
}
]
]

to (No Square brackets splitting as array) since I needed grouped as I always get in the path split ({{ $node[“seance”].json[“0”][“seance_length”] }}

{
"seance_length": 3900,
},
{
"seance_length": 3000,
},
{
"seance_length": 3900,
},
{
"seance_length": 1800,
}

Trying to follow this similar approach, but seems not that simple in my case:

Thank you very much in advance.

Could you try this out:

4 Likes

Hi @shrey-42 ,
It didn’t work for me, I don’t have a “seance” “object group” since the Json response is flat and when removing to just JSON, it states:

ERROR: seance_length.map is not a function

TypeError: seance.map is not a function

I tried to adapt to:

const result = []

const seance_length = items[0].json

for (const seance of seance_length) {
   result.push({ json: seance })
}

return result;

But I get only the first two results (Only flat data from first ) but the
{
“seance_length”: 3900,
},
{
“seance_length”: 1800,
}

Are still missing, so it only worked on first result set. Not sure if there is way of sharing workflow privately as I have API keys disclosed.

@Jorge_M

Would be preferable to have a look at your workflow.

If the API parameters are set in Credentials, then they won’t get passed on when you copy/paste the workflow.
If they are mentioned statically, you can obfuscate them after pasting here and before submitting the message.

1 Like

Hi @shrey-42 ,
Here you go:
I get first the dates and then get the seance_length

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return items[0].json.booking_dates.map(booking_dates => { return { json: { booking_dates } } });\nreturn items;\n"
      },
      "name": "APIanswer",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1171,
        334
      ]
    },
    {
      "parameters": {
        "path": "yclients",
        "responseMode": "lastNode",
        "responseData": "allEntries",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        631,
        334
      ],
      "webhookId": "63792496-dda3-43cd-bd17-f2cd0349f4ae"
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "=https://api.yclients.com/api/v1/book_dates/",
        "options": {}
      },
      "name": "dateHTTP",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        767,
        334
      ],
      "credentials": {
        "httpHeaderAuth": "yclient"
      }
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "=https://api.yclients.com/api/v1/book_times//0/{{$json[\"booking_dates\"]}}",
        "options": {}
      },
      "name": "seance",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1341,
        334
      ],
      "credentials": {
        "httpHeaderAuth": "yclient"
      }
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "booking_dates",
              "value": "={{ $node[\"dateHTTP\"].json[\"booking_dates\"] }}"
            }
          ]
        },
        "options": {}
      },
      "name": "dates",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        981,
        334
      ]
    },
    {
      "parameters": {
        "functionCode": "const result = [];\nconst timeitems = items[0].json;\nfor(const time of timeitems) {\n  time.map(i => ({json:i})).forEach(j => result.push(j));\n}\nreturn result;\n"
      },
      "name": "Flat JSON to Join dates with seance",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1530,
        330
      ]
    }
  ],
  "connections": {
    "APIanswer": {
      "main": [
        [
          {
            "node": "seance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "dateHTTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "dateHTTP": {
      "main": [
        [
          {
            "node": "dates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "seance": {
      "main": [
        [
          {
            "node": "Flat JSON to Join dates with seance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "dates": {
      "main": [
        [
          {
            "node": "APIanswer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you very much.

Hi @Jorge_M , it’s still unclear to me what’s exactly the output of the ‘seance’ node
image
as the HTTP request required authentication.

But, if it’s exactly like this:

then the above code should work fine.

Hi @shrey-42 ,
No, I need to remove the [] from it and have all in one place, because I when I add the dates from the first get request it only groups for the first date and not for all dates.

So response would have to be without []

{
“seance_length”: 3900,
},
{
“seance_length”: 3000,
},
{
“seance_length”: 3900,
},
{
“seance_length”: 1800,
}

Thank you :slight_smile:

Could you take a complete screenshot of the output of the HTTP (seance) node?

1 Like

Yes of course, and much appreciate for the help, I can make a quick loom video also to maybe show a bit better the end to end flow results maybe it is clearer.

Hi,
Here you go: https://drive.google.com/file/d/17HEjl3202waEtuyp1AFsdr-owMO7u1LB/view?usp=sharing
And thanks once again.

1 Like

Try this:

1 Like

Thanks @shrey-42 , but I get this error now:

ERROR: No data got returned. Always return an Array of items!

Error: No data got returned. Always return an Array of items!
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:71:19)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:416:47

Is there a way to maybe share the flow privately?

That’s okay.
If you go through the code in this node, you’ll see there are 2 options to select from.
You have to uncomment one of the return statements, based on your preference.

3 Likes

Haha Friday Brain fart sorry. IITT’s ALIVEE :slight_smile: . This is really great. I cannot express here my gratitude to you, thank you very much. Have a great weekend.

3 Likes