Splitting JSON lines to separate baches

Hello guys,
I need a helping hand on quite simple flow. While I am weak on Javascript and don’t want to use OpenAI for data transformation, maybe some of you will have a solution.

I have a list of objects in my JSON

I need to split it and run each through a FILTER to remove those which doesn’t meet the boolean condition. In this sample, only one object has TRUE condition and at the end of the flow, I want to run each of TRUEs separately to the next nodes.

How do you imagine that needs to be implemented?

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Saravicius
could you please post the objects as json?

the structure is hard to guess out of your image.

Cheers
Nico

Here is a structure. Marked a parameter value I’m catching

I assume that your struture is like this:

[
  {
    "287657": {
      "roomId": 1,
      "propId": 1,
      "roomsavail": 0
    },
    "287658": {
      "roomId": 1,
      "propId": 1,
      "roomsavail": 0
    }
  }
]

But to be sure I need the json data. you can get it from your node when you switch to json tab:

Oh… sorry for not being accurate. Here is a JSON

[
  {
    "287657": {
      "roomId": "287657",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287667": {
      "roomId": "287667",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287668": {
      "roomId": "287668",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287669": {
      "roomId": "287669",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287670": {
      "roomId": "287670",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287671": {
      "roomId": "287671",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287672": {
      "roomId": "287672",
      "propId": "128153",
      "roomsavail": 1
    },
    "287673": {
      "roomId": "287673",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287674": {
      "roomId": "287674",
      "propId": "128153",
      "roomsavail": "0"
    },
    "287675": {
      "roomId": "287675",
      "propId": "128153",
      "roomsavail": "0"
    },
    "checkIn": "20231122",
    "lastNight": "20231122",
    "checkOut": "20231123",
    "propId": 128153
  }
]

Okay, then the heavy lifting is to map your data to a proper list.
This is currently only possible with a bit of code.
After having a strutured list it is easy to filter for your attribute.

This is the clunky code to do the transformation:

let returnObjects = [];

// Loop over input items and add a new field called 'myNewField' to the JSON of each one
for (const item of $input.all()) {

  const nestedObjects = Object.keys(item.json)
    .filter(key => typeof item.json[key] === 'object')
    .map(key => ({ id: key, ...item.json[key] }))
    .map(object => {
      object.nestedData = {
        "checkIn": item.json.checkIn,
        "lastNight": item.json.lastNight,
        "checkOut": item.json.checkOut,
        "propId": item.json.propId
      } 
      return object;
    });
    

 returnObjects = [...returnObjects, ...nestedObjects]
  
}

return returnObjects;

And this is the whole workflow with filtering:

Cheers.

2 Likes

Dude thanks a lot, that was super smooth - connected your part and everything works! :grinning:

2 Likes

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