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

Modify Parts of JSON Document

See more JSON Examples

Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
   "fruit": [
      	{
         "kind": "apple",
	 "count": 24,
	 "fresh": true,
	 "extraInfo": null,
	 "listA": [ "abc", 1, null, false ],
	 "objectB": { "animal" : "monkey" }
      	},
	{
         "kind": "pear",
	 "count": 18,
	 "fresh": false,
	 "extraInfo": null
	 "listA": [ "xyz", 24, null, true ],
	 "objectB": { "animal" : "lemur" }
	}
    ],
    "list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
    "alien" : true
}

Chilkat Rust Downloads

Rust

let json = chilkat::JsonObject::new();

// Load the JSON from a file.
if json.load_file("qa_data/json/modifySample.json").is_err() {
    println!("{}", json.last_error_text());
    return;
}

// This example will not check for errors (i.e. null / false / 0 return values)...

// Get the "list" array:

let list_a = chilkat::JsonArray::new();

let _ = json.array_of2("list", &list_a);

// Modify values in the list.

// Change banana to plantain
let _ = list_a.set_string_at(0, "plantain").is_ok();

// Change 12 to 24
let _ = list_a.set_int_at(1, 24).is_ok();

// Change true to false
let _ = list_a.set_bool_at(2, false).is_ok();

// Is the 3rd item null?
let b_null = list_a.is_null_at(3);

// Change "orange" to 32.
let _ = list_a.set_int_at(4, 32).is_ok();

// Change 12.5 to 31.2
let _ = list_a.set_number_at(5, "31.2").is_ok();

// Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
// Do this by deleting, then inserting a new object at the same location.
let _ = list_a.delete_at(6).is_ok();
let ticker_obj = chilkat::JsonObject::new();
let _ = list_a.add_object_at2(6, &ticker_obj);

let _ = ticker_obj.append_string("ticker", "GOOG").is_ok();

// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
let _ = list_a.delete_at(7).is_ok();
let aa = chilkat::JsonArray::new();
let _ = list_a.add_array_at2(7, &aa);

let _ = aa.add_string_at(-1, "apple").is_ok();
let _ = aa.add_int_at(-1, 22).is_ok();
let _ = aa.add_bool_at(-1, true).is_ok();
let _ = aa.add_null_at(-1).is_ok();
let _ = aa.add_number_at(-1, "1080.25").is_ok();

// Get the "fruit" array
let a_fruit = chilkat::JsonArray::new();
let _ = json.array_at2(0, &a_fruit);

// Get the 1st element:
let apple_obj = chilkat::JsonObject::new();
let _ = a_fruit.object_at2(0, &apple_obj);

// Modify values by member name:
let _ = apple_obj.set_string_of("fruit", "fuji_apple").is_ok();
let _ = apple_obj.set_int_of("count", 46).is_ok();
let _ = apple_obj.set_bool_of("fresh", false).is_ok();
let _ = apple_obj.set_string_of("extraInfo", "developed by growers at the Tohoku Research Station in Fujisaki").is_ok();

// Modify values by index:

let pear_obj = chilkat::JsonObject::new();
let _ = a_fruit.object_at2(1, &pear_obj);

let _ = pear_obj.set_string_at(0, "bartlett_pear").is_ok();
let _ = pear_obj.set_int_at(1, 12).is_ok();
let _ = pear_obj.set_bool_at(2, false).is_ok();
let _ = pear_obj.set_string_at(3, "harvested in late August to early September").is_ok();

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