Skip to content
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

Modifying returned by query values #48

Open
robert-mac-wbt opened this issue Oct 31, 2023 · 2 comments
Open

Modifying returned by query values #48

robert-mac-wbt opened this issue Oct 31, 2023 · 2 comments

Comments

@robert-mac-wbt
Copy link

robert-mac-wbt commented Oct 31, 2023

Hello 👋

I wanted to ask if it's possible to do something following with this crate.
I want to be able to run multiple queries on some JSON to eventually find a node that I'd like to modify/remove.
In the following example, I'm trying to remove the genre of the second book from the list.

use std::str::FromStr;
use serde_json::{Value};
use jsonpath_rust::{JsonPathFinder, JsonPathInst, JsonPathQuery};

fn main() {
    let data = r#"
    {
        "books": [
            {
                "title": "Title 1",
                "details": {
                    "description": "Description 1",
                    "genre": "Genre 1"
                }

            },
            {
                "title": "Title 2",
                "details": {
                    "description": "Description 2",
                    "genre": "Genre 2"
                }
            }
        ]
    }
  "#;

    let json: Value = serde_json::from_str(data).unwrap();
    let books_path = JsonPathInst::from_str("$.books").unwrap();
    let finder = JsonPathFinder::new(Box::from(json), Box::new(books_path)); // had to clone due to ownership

    let books = finder.find_slice().get(0).unwrap().clone().to_data();

    let second_book = books.get(1).unwrap();
    let genre_value = second_book.clone().path("$.details.genre").unwrap(); // had to clone due to ownership in path

    // remove genre_value
}

Do you have any suggestion how to achieve this?
If I'm not mistaken find returns new Values and find_slice return reference to the Value. However none of them return mutable reference, maybe adding it could help with this?
Or maybe once the feature mentioned in #31 is added I could find the paths that are to be removed/modified, clone whole "original" json and modify it using these paths 🤔
If you have any other idea please point me to the right direction 🙏

Thanks!

@besok
Copy link
Owner

besok commented Oct 31, 2023

Hi!
It seems the issue is connected to that one #12.

Yeah, it seems, the existing API does not allow doing that.
We need to add the mutable ref.

I will look into it in a couple of weeks apparently.

@besok
Copy link
Owner

besok commented Oct 31, 2023

Update by path

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

No branches or pull requests

2 participants