Skip to content

Ability to remove JSON fields using JSONPath #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2025
Merged

Conversation

Vardhaman619
Copy link
Contributor

This PR adds deletion capabilities to the jsonpath-rust crate by implementing delete_by_path() and delete_single() methods in the Queryable trait. #105

New Methods

  • delete_by_path(path: &str) -> Result<usize, JsonPathError>
    • Deletes all elements matching the given JSONPath
    • Returns the number of elements successfully deleted
    • Handles complex queries with filters (e.g., $.users[?(@.age > 30)])
  • delete_single(path: &str) -> Result<bool, JsonPathError>
    • Deletes a single element at the specified path
    • Returns true if an element was deleted, false otherwise
    • Optimized for simple, direct path deletions

Usage Examples

use serde_json::json;
use jsonpath_rust::JsonPath;

let mut data = json!({
    "users": [
        {"name": "Alice", "age": 30, "active": true},
        {"name": "Bob", "age": 25, "active": false},
        {"name": "Charlie", "age": 35, "active": true}
    ]
});

// Delete all inactive users
let deleted_count = data.delete_by_path("$.users[?(@.active == false)]")?;
assert_eq!(deleted_count, 1);

// Delete a specific array element
let deleted = data.delete_single("$.users[0]")?;
assert_eq!(deleted, true);

// Delete object fields
data.delete_by_path("$.users[*].age")?;

@besok
Copy link
Owner

besok commented Jul 28, 2025

First of all, thank you for the PR. I think it is gonna be usefull so let's try to merge it in.

// Sort deletions to handle array indices correctly (delete from end to start)
deletions.sort_by(|a, b| {
// First sort by path depth (deeper paths first)
let depth_cmp = b.path_depth().cmp(&a.path_depth());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be done in one seq

 deletions.sort_by(|a, b|
            b.path_depth().cmp(&a.path_depth()).then_with(|| {
                match (a, b) {
                    (
                        ArrayIndex { index: idx_a, .. },
                        ArrayIndex { index: idx_b, .. },
                    ) => idx_b.cmp(idx_a),
                    _ => std::cmp::Ordering::Equal,
                }
            })
        );


/// Deletes a single element at the given path
/// Returns true if an element was deleted, false otherwise
fn delete_single(&mut self, path: &str) -> Result<bool, JsonPathError>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think would be the cases when this method is needed?
For now it throws exception if it does not get slice or index,

@besok
Copy link
Owner

besok commented Jul 28, 2025

added a couple of stylistic thinks to discuss

- Move delete_by_path from separate trait to Queryable trait
- Use Queried<usize> type alias instead of Result for consistency
- Remove delete_single method in favor of unified delete_by_path
- Simplify error handling by preserving original query errors
@besok besok merged commit b0954bc into besok:main Jul 30, 2025
5 checks passed
@besok
Copy link
Owner

besok commented Jul 30, 2025

thank you for the pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants