Oh my god. It's full of code!

queryToJson Big Improvements!

Hey all.
One of the few things I’ve managed to give back to the community is my queryToJson apex class, page and example tool. This allows you to host this simple visualforce page on a Salesforce site, send it valid SOQL queries, and get the results back in JSON. I just made some pretty awesome upgrades to it, and it is available for download on the projects page.

– All values are returned URL encoded. This ensures the returned content is always valid JSON.
– Consistent returning of the returnStringSet variable. This means that even if there is an error while processing, that is still returned. So if your app depends on it, it won’t error. It will just be empty.
– Names of queried columns returned in columns key.
– Number of columns queried returned in numColumns key.
– Status of request returned in success key (boolean true or false for easy evaluation).
– Number of records returned in records key.
– Empty values are returned, instead of being removed.

Also, the example code has been updated. It now shows how to analyze the status of the request, iterate over all the queried fields and put the requested data in a nifty table. So download it and check it out.

Example of use

3 responses

  1. I realize this is kind of an old post to reply to, but you seem to know what you’re doing. I’m cutting my teeth on this whole apex/visual force thing and am trying to create a JSON page with the results from a simple SOQL query. I can get the first result no problem, but I need to have it iterate through the rows (if that’s even the right term, I’m coming from PHP/MySQL). Any insights? Feel free to Facebook message me.


    July 27, 2011 at 4:32 am

    • Hey Chris,
      So at what point are you trying to do your iteration? You have queried the data and have the records in a list of sObjects at this point correct? If you are just trying to iterate over that list it’s as simple as

      for(sObject obj : listName)


      If you are talking about iterating over the JSON data, that depends on what language is consuming the JSON you have generated. In most cases it would be javascript. Apex does not have any kind of JSON parser built in, so if you are trying to iterate over JSON values in Apex, you may be out of luck. Let me know a bit more about where you are at in your code and I can try and help some more.

      July 28, 2011 at 2:58 am

  2. First of all I want to say superb blog! I had a quick question in
    which I’d like to ask if you do not mind. I was interested to find out how you center yourself and clear your head before writing. I have had a hard time clearing my thoughts in getting my ideas out. I do enjoy writing however it just seems like the first 10 to 15 minutes are usually lost just trying to figure out how to begin. Any ideas or tips? Appreciate it!

    January 17, 2013 at 9:46 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s