I had to write a library to transform JSON from one shape into another, carrying over the values but placing them in different nodes on a new tree. I found that thinking in terms of tree transformations was incredibly hard. The way I solved it was by flattening the JSON, transforming the flattened version, and when I'm done, inflating it back up.
In other words, working with the simpler, "2D" version of a JSON was much simpler than its "3D" tree structure, and writing flatten/unflatten independent from the transforming code was a nice modularity bonus.
It's really all about decomposing the problem and solving its parts.
It sounds like you could have benefited from a way to express pattern matching using JSON syntax, for destructuring a JSON object, and to represent the synthesis of new JSON using a "JSON quasiquote".
E.g. Lisp transformation with classic destructuring-bind and backquote:
(destructuring-bind (a (b (c &rest d) e) f (g h)) some-obj
`((,a ,b) ,c (,@d 1 2 3 ,e) (,f ,g, h)))
There are pattern matching libraries nowadays that do a lot more than destructuring-bind, but it illustrates the basic point.The destructuring-bind macro writes the code to pull apart some-obj according to the tree picture with embedded variables. The backquote syntax generates the code whose evaluation synthesizes the new tree object according to a template, with the values of expressions indicated by , and ,@ substituted and spliced into the template.
It did get me thinking, though, so I appreciate the advice. I'm wondering now how to port that idea to Ruby.
(Why almost: because "destructuring lambda lists" don't support the environment parameter of macro lambda lists!)
ps: I rarely hear about http://en.wikipedia.org/wiki/L_Peter_Deutsch but his work is impressive.
(But on larger machines in the 70s I get the impression Interlisp stayed with structure-editing for its advantages, not just inertia. It was killed off by Common Lisp which owed more to the east-coast Lispers who gave us Emacs.)
I thought emacs text buffer genes came from being implemented on and for *nix ..
There are some difficulties in this, of course. Sometimes I don't think in terms of trees of information. This is relatively rare, though.
This is quite an amazing article in that the author really "gets" Lisp and yet at the same time doesn't get it.
(It isn't mentioned; don't waste time looking.)
If you admit you're already thinking in terms of that tree structure already, just freakin' use it instead of this charade of encoding into in some completely rearranged way that has to be unraveled back to what you were originally thinking of anyway (or something else).