Solved

Creating a segment based on a complex JSON Array of Objects Attribute

  • 8 December 2022
  • 3 replies
  • 167 views

I am trying to create a segment using an attribute which is a complex array of objects, which also include nested objects.

For testing I have creating an attribute with the following data:

[
{
"id": 1,
"active": true,
"language": "de"
},
{
"id": 2,
"active": false,
"language": "fr"
},
{
"id": 3,
"active": false,
"language": "en"
}
]


I want to segment based on the “active language” which means the language key of the object where the active key is set to true. So in the example above, the active language would be de.

Based on the documentation here: https://www.customer.io/docs/json-in-segments/ 

It doesn’t seem like there is a way to segment based on 2 keys within the same object, meaning that when I create the following segments for my active languages, this test user appears in all segments.

 


Is there a way to set up the segments so that this test user would only appear in the de segment and not in the otheres.​​​​

icon

Best answer by Ramy 9 December 2022, 13:21

View original

3 replies

Userlevel 1
Badge

Hi Jan!


It is possible for you to build conditions that check for two (or more) keys within the same object. To clarify, you can construct Segment Conditions to look for:

test_attribute[0].active is equal to true
and
test_attribute[0].language is equal to fr

OR

test_attribute[1].active is equal to true
and
test_attribute[1].language is equal to fr

OR

test_attribute[2].active is equal to true
and
test_attribute[2].language is equal to fr

In this scenario, the first joined condition looks for the first object having both those values on those keys, failing that, the second joined condition looks for both values on the keys for the second object, and so on. The tip to do this is all about specifying the index number on each condition, so we limit our Segmentation Engine to look only within that numbered index.

Note, the first object is always at index [0].

I have to say, the validity of this solution would mainly depend on how many objects you expect this attribute to contain at maximum. Seeing this is a language attribute, I would assume this could hold 3-4 ‘langauge’ objects at max. However, if you expect some attributes to contain more objects, you’ll want to account for that number in your Segement Conditions, by adding this set of condition for the expected number of objects.

Hi Ramy, 

Thanks for your reply. 

Unfortunately that solution won’t work since I won’t have an expected number of objects this could be 1 to n, where n is any number of objects.

It also isn’t really scalable to set up the segments this way if many segments need to be created based off of these nested objects.

Userlevel 1
Badge

Hi Jan!

 

Thank you for following up on this thread.

Sorry to hear that workaround wouldn’t work for your use case, although I surely understand the scalability concern. JSON Segmentation is still quite a new feature, which is why our Product Team is always interested in learning what use cases our customers come up with to utilize this feature. I have to say this use case is a particularly clever one that can potentially help a lot!

I have shared this use case with the Product team. While I’m afraid there is no concrete timeline we can share for this feature request, we’ll be sure to get in touch once this change is worked on and gets released into Production Environments.

Apologies for not bearing you better news on this, Jan!

Reply