I'm trying to pass an array of strings from JavaScript to apex. I am initially converting the array into a JSON object and then stringifying the object and passing it into my apex code as a string. So far this works but I am encountering an error when trying to convert my JSON string into a list of string. Below is my code samples:

<apex:page controller="testController" id = "pageId">
    <apex:form id="form">
        <button type="button" onclick="passToApex()">Pass Array to Apex class</button>
        <apex:inputHidden value="{!stringIN}" id="hiddenfield"/>
        <apex:actionFunction name="stringINName" action="{!passedInMethod}" rerender="Form"/>
        <apex:pageblockTable value="{!StringValues}" var="SList" rendered="{!StringValues != null}"></apex:pageblockTable>
    <script type="text/javascript">
        function passToApex(){
            var J = {"0": "String 'One' ","1": "String 'Two' ","2": "String 'Three' "};
            var JStr = JSON.stringify(J);
            document.getElementById('{!$Component.pageId.form.hiddenfield}').value = JStr;


public with sharing class testController {
    public String stringIN{get; set;}
    public list<string> strList = new list<string>();
    public set<string> StrSet = new set<string>();
    public pageReference passedInMethod(){
        return null;
    public set<string> getStringValues(){
            list<string> strList = (List<String>)JSON.deserialize(stringIN,  List<String>.class);
        }catch (System.NullPointerException e){

        if(StrSet.size() > 0){
            return StrSet;
        return null;

Essentially I would like to convert the JSON in a string set in the form : {"String 'One'", "string 'Two'", "string 'Three'"}

What am I not doing right that may be causing this error? Thanks.

  • 1
    The apex json.deserialize(..) is expecting the stringIn to look like [ "aa", "bb", "cc" ] if you are going to deserialize to a list of strings
    – cropredy
    Jan 30, 2015 at 18:17
  • What do you want your array to look like? in your example, it appears you have three 0 objects, is what what you expect? Or is the array supposed to be the Strings? Something like this: {"0": ["String 'One' ", "String 'Two' ", "String 'Three' "]}
    – JimRae
    Jan 30, 2015 at 18:48
  • @JimRae - essentially I would like it to be something like this : {"String 'One'", "string 'Two'", "string 'Three'"}. Jan 30, 2015 at 18:58
  • PS - It's bad practice to throw away an exception like that catch block. Jan 30, 2015 at 19:15

2 Answers 2


JSON arrays are simply written as '[ "Item1", "Item2", "Item3" ]'.

As for generating the correct array, you should be using JavaScript built-ins, instead of trying to write your own JSON by hand:

var inputs = [];
inputs.push("String 'One'")
inputs.push("String 'Two'")
inputs.push("String 'Three'")
document.getElementById('{!$Component.pageId.form.hiddenfield}').value = JSON.stringify(inputs);
  • I am actually building my array dynamically based on certain user input and then building a JSON object from it (based on what you said I realize that my building of JSON object from my array part was unnecessary and was where I went off track.) However just stringifying the array and sending it to my apex and trying to deserialize it gives another error : "System.JSONException: No content to map to Object due to end of input". Jan 30, 2015 at 19:20

Your problem is that the string you are trying to parse is not JSON. In Javascript Object Notation, curly braces represent "Objects" - really a hashmap of keys to values.

In order to get an Array, you need to use square braces.

Note that JSON is simply a strict subset of Javascript.

E.g., Object (hash):

  key1: obj1,
  key2: obj2



In both examples, objX can be a String, Array, Object (i.e., hash), Number, true, false or null. Each key, however, must be a String.

Here's a JSON Validator if you're interested.

  • Thanks Charles. I was also going to handle the exception later once I had fixed all my JSON issues. Jan 30, 2015 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .