Sample code for 30+ languages & platforms
Rust

JSON Copy Objects

See more JSON Examples

Copy objects from one JSON document to another.

Chilkat Rust Downloads

Rust
let json1 = chilkat::JsonObject::new();
let _ = json1.update_string("ID1.cn", "Name");
let _ = json1.update_string("ID1.objectGUID", "GUID");
let _ = json1.update_string("ID2.cn", "Name");
let _ = json1.update_string("ID2.objectGUID", "GUID");

json1.set_emit_compact(false);
println!("{}", json1.emit().unwrap_or_default());

// json1 contains:
// {
//   "ID1": {
//     "cn": "Name",
//     "objectGUID": "GUID"
//   },
//   "ID2": {
//     "cn": "Name",
//     "objectGUID": "GUID"
//   }
// }

let json2 = chilkat::JsonObject::new();
let _ = json2.update_string("Name1.ID1.cn", "Name");
let _ = json2.update_string("Name1.ID1.objectGUID", "GUID");
let _ = json2.update_string("Name1.ID2.cn", "Name");
let _ = json2.update_string("Name1.ID2.objectGUID", "GUID");
let _ = json2.update_string("Name2.ID3.cn", "Name");
let _ = json2.update_string("Name2.ID3.objectGUID", "GUID");

json2.set_emit_compact(false);
println!("{}", json2.emit().unwrap_or_default());

// {
//   "Name1": {
//     "ID1": {
//       "cn": "Name",
//       "objectGUID": "GUID"
//     },
//     "ID2": {
//       "cn": "Name",
//       "objectGUID": "GUID"
//     }
//   },
//   "Name2": {
//     "ID3": {
//       "cn": "Name",
//       "objectGUID": "GUID"
//     }
//   }
// }

// Copy Name1, Name2 into json1

let mut i = 0;
let num_members = json2.size();
while i < num_members {
    let json_obj = json2.object_at(i).unwrap();
    let _ = json1.append_object_copy(&json2.name_at(i).unwrap_or_default(), &json_obj);
    i = i + 1;
}

// Now see what json1 contains:
println!("{}", json1.emit().unwrap_or_default());

// {
//   "ID1": {
//     "cn": "Name",
//     "objectGUID": "GUID"
//   },
//   "ID2": {
//     "cn": "Name",
//     "objectGUID": "GUID"
//   },
//   "Name1": {
//     "ID1": {
//       "cn": "Name",
//       "objectGUID": "GUID"
//     },
//     "ID2": {
//       "cn": "Name",
//       "objectGUID": "GUID"
//     }
//   },
//   "Name2": {
//     "ID3": {
//       "cn": "Name",
//       "objectGUID": "GUID"
//     }
//   }
// }