How to get the index of a dynamic nested array by the property value?

Hey there!

Need some help to get a dynamic data integration.

I have this worflow steps:

1 - Get students data from Notion database
2 - Get student progress on Teachable (course platform)
3 - Update progress status on Notion database

image

This is the notion Database (one column for each course)

I get this JSON of Teachable API return (step 2 of this workflow). This exemple I have 2 students (Bruno1 and Bruno 2), each one with different enrolled courses. When the student enroll in a course, “courses” array gets the enrolled information, so this is a dynamic array as we can see here:

[
  {
    "email": "[email protected]",
    "name": "Bruno1",
    "src": null,
    "role": "student",
    "last_sign_in_ip": "2804:14d:783a:808b:dda:bd3f:8315:c567",
    "id": 75427844,
    "courses": [
      {
        "course_id": 1776448,
        "course_name": "Curadoria Bot IA/NLU",
        "enrolled_at": "2022-07-17T15:50:18Z",
        "is_active_enrollment": false,
        "completed_at": null,
        "percent_complete": 0
      },
      {
        "course_id": 1359830,
        "course_name": "Botcamp - Watson",
        "enrolled_at": "2022-07-17T15:50:06Z",
        "is_active_enrollment": true,
        "completed_at": null,
        "percent_complete": 0
      },
      {
        "course_id": 1654315,
        "course_name": "Hello Bot",
        "enrolled_at": "2022-07-15T22:33:12Z",
        "is_active_enrollment": true,
        "completed_at": null,
        "percent_complete": 74
      }
    ]
  },
  {
    "email": "[email protected]",
    "name": "Bruno2",
    "src": null,
    "role": "student",
    "last_sign_in_ip": null,
    "id": 74748672,
    "courses": [
      {
        "course_id": 1654315,
        "course_name": "Hello Bot",
        "enrolled_at": "2022-06-27T03:09:36Z",
        "is_active_enrollment": true,
        "completed_at": null,
        "percent_complete": 15
      },
      {
        "course_id": 1654316,
        "course_name": "Botcamp - Dialogflow",
        "enrolled_at": "2022-06-27T03:09:37Z",
        "is_active_enrollment": true,
        "completed_at": null,
        "percent_complete": 0
      }
    ]
  }
]

The challenge is, I have dynamic array from one side and I need to connect it a fixed database column in Notion. Trying to do that connection, I need to get the “percent_complete” field, but not of a fixed array index as shown below

What I need is to find in which array index the course is, check if the user is really enrolled (“is_active_enrollment”: true) and then integrate this JSON field to Notion Node.

Since n8n is based on JMESPath, I tried to create an expression that filters the JSON, using this exemple found in documentation (JMESPath Examples — JMESPath). This one:

But no success…

Is there any elegant way to do that?

I need to loop each student, them loop each student course to update each status on Notion.

How to get the index of a dynamic nested array by it property value?

Hope you can assist me on this challenge!
Tkss!

Hi @brunofazoli,
welcome to the community :tada:

I tried building your jmespath like this and it seems to work.

{{$jmespath($json.courses,"[?course_id==`1776448`].percent_complete")[0] || 0}}

Here is my sample workflow:

I also created an alternative workflow using our Item Lists Node to split out the courses field.

1 Like

It worked perfectly! Thank you!