4

I use a REST Apex service and return String from the method. The client call to the REST API returns with qoutes around the reqult like this example: "...result.." and also with \" inside the result.How do I get rid of the quotes ? I saw Pat's blog about using Blob.valueOf(..) but I want to return a JSONObject like a String as you would do in Jersey.

@HttpGet
global static String getAllEmployeeDataForContact() { 
       return JSON.serialize(.....)

TIA

4
  • Have you tried setting the correct JSON headers? RestResponse.addHeader('Content-Type', 'application/json');
    – pjcarly
    Nov 8, 2012 at 8:43
  • Adding Content-Type helped but I thought by default all calls returned JSON but that doesn't seem to be true if I return String. Using Pat's Blob.valueoF with RestResponse.addHeader('Content-Type', 'application/json'); worked.
    – Vijay
    Nov 8, 2012 at 15:35
  • great to hear, perhaps best to mark metadaddy his anwser as correct and add the headers to it
    – pjcarly
    Nov 9, 2012 at 10:23
  • @PJC I think what Vijay is actually trying to do is return an SObject or Apex Class object. I've updated my answer to give the correct method for doing both.
    – metadaddy
    Nov 9, 2012 at 16:26

3 Answers 3

4

Quotes will come anyway, you don't need to use JSON.serialise with Apex web REST services, they take care of format XML/JSON automatically, so returning the type directly would work. Here is the sample, that I tried and is working fine.

@RestResource(urlMapping='/stacke/*')
global class SampleWebService {

    @httpget
    global static String getAllEmployeeDataForContact() { 
           return 'hello world';
    }

}

Result is coming with double quotes as you said and its correct as well

"hello world"

But this is not a JSON string actually try converting it to some wrapped type for easy consumption by clients like

@RestResource(urlMapping='/stacke/*')
global class SampleWebService {

    global class Result {
        global String response{get;set;}
    }
    @httpget
    global static Result getAllEmployeeDataForContact() { 
            Result r = new Result();
            r.response = 'hello world';
           return r;
    }

}

This returns a JSON string like this

{
    "response": "hello world"
}

Hope this helps.

1
16

To return a string without the quotes, set it as the response body:

@HttpGet
global static void sayHello() {
    RestContext.response.addHeader('Content-Type', 'application/json');
    // Return a single account as an example
    RestContext.response.responseBody = 
        Blob.valueOf(JSON.serialize((Account)[SELECT Name, Id FROM Account LIMIT 1]));
}

Result:

$ curl -H "X-PrettyPrint: 1" https://superpat-developer-edition.na3.force.com/services/apexrest/superpat/HelloService
{"attributes":{"type":"Account","url":"/services/data/v26.0/sobjects/Account/0015000000f2cuKAAQ"},"Name":"Accounts R You","Id":"0015000000f2cuKAAQ"}

Having said that, there is a better way to return a JSON-encoded SObject or Apex Class object. Simply define the REST method to return the relevant type. For example, an SObject:

@RestResource(urlMapping='/AccountService/*')
global class AccountService {
    @HttpGet
    global static Account getAccount() { 
        // return an account
        return [SELECT Name, Id FROM Account LIMIT 1];
    }
}

Yielding

$ curl -H "X-PrettyPrint: 1" https://superpat-developer-edition.na3.force.com/services/apexrest/superpat/AccountService
{
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v26.0/sobjects/Account/0015000000f2cuKAAQ"
  },
  "Name" : "Accounts R You",
  "Id" : "0015000000f2cuKAAQ"
}

or an Apex class:

@RestResource(urlMapping='/MyService/*')
global class MyService {
    global class MyClass {
        public String s;
        public Integer i;

        public MyClass(String s, Integer i) {
            this.s = s;
            this.i = i;
        }
    }

    @HttpGet
    global static MyClass sayHello() { 
        MyClass obj = new MyClass('Hello', 1234);

        return obj;
    }
}

giving

$ curl -H "X-PrettyPrint: 1" https://superpat-developer-edition.na3.force.com/services/apexrest/superpat/MyService
{
  "s" : "Hello",
  "i" : 1234
}

Or even collections of either of the above:

@RestResource(urlMapping='/AccountsService/*')
global class AccountsService {
    @HttpGet
    global static List<Account> getMatchingAccounts() {
        String prefix = RestContext.request.params.get('prefix');
        String filter = prefix + '%';
        return [SELECT Name, Id FROM Account WHERE Name LIKE :filter];
    }
}

$ curl -H "X-PrettyPrint: 1" https://superpdeveloper-edition.na3.force.com/services/apexrest/superpat/HelloService?prefix=Unit
[ {
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v26.0/sobjects/Account/0015000000VALDuAAP"
  },
  "Name" : "United Oil & Gas, UK",
  "Id" : "0015000000VALDuAAP"
}, {
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v26.0/sobjects/Account/0015000000VALDvAAP"
  },
  "Name" : "United Oil & Gas, Singapore",
  "Id" : "0015000000VALDvAAP"
}, {
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v26.0/sobjects/Account/0015000000VALE3AAP"
  },
  "Name" : "United Oil & Gas Corp.",
  "Id" : "0015000000VALE3AAP"
} ]
3
  • check comments on the question please
    – pjcarly
    Nov 9, 2012 at 10:24
  • Checked comments on the question and expanded my answer :-)
    – metadaddy
    Nov 9, 2012 at 16:47
  • Thanks, Pat (@metadaddy) - exactly what I needed to know!
    – shadit
    Aug 5, 2014 at 16:37
0

Have you seen this?

https://stackoverflow.com/questions/9833992/apex-json-generator-writestring-escapes-quotes

It looks like it might solve your problem.

You must log in to answer this question.

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