improve/fix query/document_to_json functions