Find your content:

Search form

You are here

JSON String in response shows up with quotes

 
Share

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


Attribution to: Vijay

Possible Suggestion/Solution #1

Have you seen this?

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

It looks like it might solve your problem.


Attribution to: paul

Possible Suggestion/Solution #2

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.


Attribution to: Abhinav Gupta

Possible Suggestion/Solution #3

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"
} ]

Attribution to: metadaddy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4174

My Block Status

My Block Content