How can I make a list of objects uniform (ensure same properties)?

Use the Patch JSON List Node to make a list of objects uniform

Often, when working with a list of items, not every one of the items has the same properties. This is for example the case, when requesting Insights from the Facebook API for a list of adsets. It might be, that some of the objects have a value for “revenue”, but others don’t.

 

If this is the case, and you want to filter the list by revenue, you will run into problems. So in order to work with the data, we need to make sure all items have the same properties.

 

Take for example this demo data:

[
  {
    "id": "fsjd25fk6jfns",
    "name": "Demo Adset 1",
    "spend": 157,
    "revenue": 87
  },
  {
    "id": "as2dfh23hfw3r8",
    "name": "Demo Adset 2",
    "spend": 17
  },
  {
    "id": "wjef6ijs3c4rw",
    "name": "Demo Adset 3",
    "spend": 120
  }
]
 

While the first item has the property “revenue”, the other two don’t. If we now want to filter this list by items with a revenue > 20, we would run into an error.

 

This can easily be mitigated by using the Patch JSON List Node. One of the available patches is the Test patch. This lets us test, wether a property exists or has a certain value. All other patches that come after the Test patch, will then only be applied if the Test patch returns true.

Notion image

So, in order to check, if a property does not exist, and if that is the case, add the property - we can set it up as shown in the example above.

The Test patch checks if the property revenue is equal to null - which is the case, if it doesn’t exist. In those cases, the Add patch then adds the property with a value of 0.

After running this, the data will look like this:

[
  {
    "id": "fsjd25fk6jfns",
    "name": "Demo Adset 1",
    "spend": 157,
    "revenue": 87
  },
  {
    "id": "as2dfh23hfw3r8",
    "name": "Demo Adset 2",
    "spend": 17,
    "revenue": 0
  },
  {
    "id": "wjef6ijs3c4rw",
    "name": "Demo Adset 3",
    "spend": 120,
    "revenue": 0
  }
]
 
Did this answer your question?
😞
😐
🤩

Last updated on November 1, 2022