October 30, 2012

Generating JSON in Visualforce page in Salesforce

JSON stands for “Java Script Object Notation“.

JSON.serialize() is used to generate JSON. It is a lightweight data-interchange format.

JSON is built on two structures:
  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
Sample Code:

Visualforce page:

<apex:page controller="sample" action="{!parseJson}">

Apex Controller:

public class sample
    public String text1 {get;set;}
    public sample()
    public void parseJson()
        String soql = 'SELECT Name FROM Account LIMIT 5';
        List<Account> acct = Database.Query(soql);
        text1 = JSON.serialize(acct);


JSON.deserialize() method is used to deserialize the specified JSON string into an Apex object of the specified type.


  1. can u please rectify this error

    Error: json Compile Error: Method does not exist or incorrect signature: JSON.serialize(LIST) at line 13 column 17

    Thank u

    1. Check your code properly.

      Its working fine for me.

      Cheers !!!

    2. Hi how can i get the above VF page values in to a table ??

  2. i have tried the code exactly what you have given but it showing the error any ways thank u for mainting this blog