Skip to content

Can I merge two JSON objects using this library? #377

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

Closed
harindaka opened this issue Nov 2, 2017 · 4 comments
Closed

Can I merge two JSON objects using this library? #377

harindaka opened this issue Nov 2, 2017 · 4 comments
Labels

Comments

@harindaka
Copy link

Let's say I have the JSON below (JSON 1)

{
  "title": "This is a title",
  "person" : {
    "firstName" : "John",
    "lastName" : "Doe"
  },
  "cities":[ "london", "paris" ]
}

And another as follows (JSON 2)

{
  "title": "This is another title",
  "person" : {
    "firstName" : "Jane"
  },
  "cities":[ "colombo" ]
}

How can I get the following output? (merging 2 into 1 where 2 Overrides 1)

{
  "title": "This is another title",
  "person" : {
    "firstName" : "Jane",
    "lastName" : "Doe"
  },
  "cities":[ "colombo" ]
}

I did checkout the crate json-patch which does this but it does not compile against stable rust. I want to know if its possible to do something similar with serde.

@cmdln
Copy link

cmdln commented Nov 2, 2017

If you parse both pieces of JSON to the serde_json::Value enum then you can call as_object_mut or use if let to destructure the values to Maps. You can then use extend on the first Map with the second Map as the argument. I believe that should result in a Map instance that matches what you describe. You can then construct a new Value::Object(map1) and serde_json::to_string or to_writer with that Value.

@dtolnay
Copy link
Member

dtolnay commented Nov 2, 2017

Here is one possible implementation. I haven't tested it thoroughly but it works for your example.

use serde_json::{json, Value};

fn merge(a: &mut Value, b: &Value) {
    match (a, b) {
        (Value::Object(a), Value::Object(b)) => {
            for (k, v) in b {
                merge(a.entry(k.clone()).or_insert(Value::Null), v);
            }
        }
        (a, b) => *a = b.clone(),
    }
}

fn main() {
    let mut a = json!({
        "title": "This is a title",
        "person" : {
            "firstName" : "John",
            "lastName" : "Doe"
        },
        "cities":[ "london", "paris" ]
    });

    let b = json!({
        "title": "This is another title",
        "person" : {
            "firstName" : "Jane"
        },
        "cities":[ "colombo" ]
    });

    merge(&mut a, &b);
    println!("{:#}", a);
}

@harindaka
Copy link
Author

@commandline Thank you for your suggestion.
@dtolnay Yours worked perfectly. Thank you!. Any Gotchas in your code I should know about? I'm still learning Rust.

@dtolnay
Copy link
Member

dtolnay commented Nov 2, 2017

I don't think there are gotchas but depending on how the rest of your code is structured and specifically depending on whether you have ownership of a and/or b, you may be able to come up with a more efficient implementation. For example if you have ownership of b then you may be able to move "This is another title" instead of cloning it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants