Switch to using the built-in encoding/json marshaller for query building (work done by Mark Boyd)

Instead of building json by hand, use the built in json encoder.

The motivation for this was to fix a bug detected in our unit tests relating to a double-quote character in an attribute value, but as a nice side effect,  the rework also reduces amount of code significantly.