Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Insert JSON Object into another JSON Object

See more JSON Examples

Demonstrates how to insert one JSON object into another. Effectively, the JSON object must be copied into the other..

Chilkat Rust Downloads

Rust
// Imagine we have two separate JSON objects.
let json_a = chilkat::JsonObject::new();
let _ = json_a.update_string("animal", "zebra");
let _ = json_a.update_string("colors[0]", "white");
let _ = json_a.update_string("colors[1]", "black");

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

// jsonA contains:

// {
//   "animal": "zebra",
//   "colors": [
//     "white",
//     "black"
//   ]
// }

let json_b = chilkat::JsonObject::new();
let _ = json_b.update_string("type", "mammal");
let _ = json_b.update_bool("carnivore", false);

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

// jsonB contains:

// {
//   "type": "mammal",
//   "carnivore": false
// }

// Let's say we want to insert jsonB into jsonA to get this:

// {
//   "animal": "zebra",
//   "info" " {
//       "type": "mammal",
//       "carnivore": false
// 	},
//   "colors": [
//     "white",
//     "black"
//   ]
// }

let _ = json_a.add_object_copy_at(1, "info", &json_b);

println!("{}", json_a.emit().unwrap_or_default());

// The result is this:

// {
//   "animal": "zebra",
//   "info": {
//     "type": "mammal",
//     "carnivore": false
//   },
//   "colors": [
//     "white",
//     "black"
//   ]
// }