input: { outer: { inner: [ {item: "one", values: [3, 2, 1]}, {item: "two", values: [1, 2, 3]}, {item: "three", values: [7, 8, 9]}, ] innernope: "innernope" } unrelated: "unrelated" } path: ["outer", "inner", 1, "values"] append: 4 breakdown: [ for idx, p in path { if idx == 0 { input[p] } if idx > 0 { breakdown[idx-1][p] } }, ] reconstruct: [ for idx, _ in path { if (int & path[len(path)-idx-1]) == _|_ { { if idx < len(path)-1 { for key, value in breakdown[len(path)-idx-2] if key != path[len(path)-idx-1] { "\(key)": value } } if idx == len(path)-1 { for key, value in input if "\(key)" != "\(path[len(path)-idx-1])" { "\(key)": value } } if idx == 0 { "\(path[len(path)-1])": breakdown[len(path)-1] + [append] } if idx > 0 { "\(path[len(path)-idx-1])": reconstruct[idx-1] } } } if (int & path[len(path)-idx-1]) != _|_ { [ for key, value in breakdown[len(path)-idx-2] { if key != path[len(path)-idx-1] { value } if key == path[len(path)-idx-1] { if idx == 0 { breakdown[len(path)-1] + [append] } if idx > 0 { reconstruct[idx-1] } } }, ] } }, ] output: reconstruct[len(path)-1] & { outer: { inner: [ {item: "one", values: [3, 2, 1]}, {item: "two", values: [1, 2, 3, 4]}, {item: "three", values: [7, 8, 9]}, ] } }