Deep Object Filter - Problem

Given a table containing JSON objects and a filter condition, perform a deep filtering operation that removes records where nested properties don't meet the criteria.

The deep filter should:

  • Extract and evaluate nested JSON properties using JSON_EXTRACT
  • Remove records where the filter condition evaluates to false
  • Handle cases where JSON properties are null or missing
  • Return only records with valid, non-empty nested data

If no records meet the criteria after filtering, return an empty result set.

Table Schema

Documents
Column Name Type Description
id PK int Primary key
data json JSON object containing nested properties
category varchar Document category
Primary Key: id
Note: Each row contains a JSON document that may have nested objects and arrays

Input & Output

Example 1 — Filter Active Adult Users
Input Table:
id data category
1 {"user":{"active":true,"age":25,"name":"John"},"settings":{"notifications":true}} profile
2 {"user":{"active":false,"age":16,"name":"Jane"},"settings":{"notifications":false}} profile
3 {"user":{"active":true,"age":30,"name":"Bob"},"settings":{"notifications":true}} profile
Output:
id data category
1 {"user":{"active":true,"age":25,"name":"John"},"settings":{"notifications":true}} profile
3 {"user":{"active":true,"age":30,"name":"Bob"},"settings":{"notifications":true}} profile
💡 Note:

Filters records where user.active is true AND user.age is >= 18. Record 2 is filtered out because the user is inactive (false) and under 18.

Example 2 — Handle Missing Properties
Input Table:
id data category
4 {"user":{"active":true,"age":22}} incomplete
5 {"settings":{"notifications":true}} incomplete
6 {} empty
Output:
id data category
💡 Note:

No records match because they either lack required nested properties or have null/missing values. The filter requires both user.active=true and user.age>=18 and non-null settings.notifications.

Constraints

  • 1 ≤ id ≤ 1000
  • data contains valid JSON objects
  • Nested properties may be null or missing

Visualization

Tap to expand
Deep Object Filter INPUT { "id": 1, "data": {"status": "active"} } { "id": 2, "data": {"status": null} } { "id": 3, "data": {"status": "done"} } Filter: data.status != null ALGORITHM STEPS 1 Parse JSON Extract nested properties 2 Navigate Path Traverse to data.status 3 Evaluate Filter Check null/missing values 4 Build Result Return valid records only Filter Evaluation: id:1 status:"active" OK id:2 status:null X id:3 status:"done" OK FINAL RESULT Filtered Array (2 records) { "id": 1, "data": {"status": "active"} } { "id": 3, "data": {"status": "done"} } Removed: id:2 (null status) Output: 2 valid records Key Insight: Deep object filtering requires recursive traversal of nested JSON properties. The algorithm navigates dot-notation paths (e.g., "data.status"), handles null/undefined gracefully, and preserves only records where nested values exist and satisfy the filter condition. Time complexity: O(n * d) where d = depth. TutorialsPoint - Deep Object Filter | Optimal Solution
Asked in
Amazon 28 Meta 19
23.4K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen