I have a doubt regarding the JSON deserialiser method.

So when we deserialise a JSON response or JSON file. Is there a way to make every attribut and value into a string type rather than its original datatype.

The reason I am asking this is because I faced some error in one-two variables in a JSON while parsing and displaying the result in my visual force page.

The error was

illegal value for primitive

Anyhelp will be appreciated. Thanks all


You cannot directly do so. As a workaround, can manually parse the JSON with JSON.deserializeUntyped or JSONParser, then do the conversion manually for each value.

Here's an example:

Object stringifyAll(Object value) {
  if(value instanceOf Map<String, Object>) {
      System.debug('parsing map');
    Map<String, Object> temp = (Map<String, Object>)value;
    for(String key: temp.keySet()) {
      temp.put(key, stringifyAll(temp.get(key)));
    return temp;
  if(value instanceOf List<Object>) {
      system.debug('parsing list');
    List<Object> temp = (List<Object>)value;
    for(Integer i = 0, s = temp.size(); i < s; i++) {
      temp[i] = stringifyAll(temp[i]);
    return temp;
  return value==null?null:value+'';

Which is called as:

Object results = stringifyAll(

Or, if you want it back in a JSON string:

String jsonResults = JSON.serialize(

If you want to retain null values as null (instead of the string "null"), change the last line to:

return value == null? null: ''+value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.