Web Connection
Escape characters in JSON responses
Gravatar is a globally recognized avatar based on your email address. Escape characters in JSON responses
  kirk allen
  All
  Aug 16, 2021 @ 10:40am

Hello.

I have a REST API that returns json data, but that data contains escape characters and I can't figure out why.

Here's a sample line with data from a field named "recd_on":

"[\r\n {\r\n "recd_on": "2020-04-30T07:00:00Z",\r\n ......"

Are those escape codes being added by the serializer or by the API client or somewhere else?

Thanks! Kirk

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  Rick Strahl
  kirk allen
  Aug 16, 2021 @ 11:32am

JSON Data returned from a service will be in almost certainly in UTF-8 encoding, so you need to decode the values.

If you're using wwHttp there the lDecodeUtf8 flag can do this automatically based on the content type of the message.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  kirk allen
  Rick Strahl
  Aug 16, 2021 @ 01:15pm

Thanks Rick.

That makes sense, but I should have been more clear. The API that is returning the escape characters with the json response is my Web Connection 6.0 REST API, so I control the API output. Can I exclude those escape characters when I'm creating my json response with the ww json serializer?

Thanks again, Kirk

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  Rick Strahl
  kirk allen
  Aug 16, 2021 @ 01:33pm

Yes. On the server side using:

*** Explicitly specify UTF-8 encoding
Response.Encoding = "UTF8"

in OnProcessInit() (or in individual methods before output is generated) will encode the entire response, regardless of how you create it.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  kirk allen
  Rick Strahl
  Aug 16, 2021 @ 02:02pm

Thanks Rick.

My OnProcessInit() method contains:

    Response.Encoding = "UTF8"
    REQUEST.lUtf8Encoding = .T.

but I'm still getting the escape characters in my responses.

The response header, as shown in WebSurge (using it to test my API), looks like:

RequestId: 17_96860053 Content-Length: 4343 Cache-Control: private Content-Type: application/json;charset=utf-8 Date: Mon, 16 Aug 2021 20:59:57 GMT Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET

Thanks for your help, Kirk

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  Rick Strahl
  kirk allen
  Aug 16, 2021 @ 09:38pm

How is your JSON generated? As part of a Web Connection API REST service using wwRestProcess? That should automatically handle encoding anything as long as your FoxPro CodePage can handle the text.

Just did a quick check with one of my application, dumping some extended characters into the data:

JSON output is properly encoded in the browser.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  kirk allen
  Rick Strahl
  Aug 17, 2021 @ 08:11am

Thanks Rick.

I figured out what I was doing. I noticed in the TestPage method that you don't need to serialize the response. You just return the object. I was serializing the response cursor first, and then returning that object, like this.

loReturnObj = loJSerializer.Serialize("cursor:crstheapps")
USE IN crstheapps
       
RETURN loReturnObj

And that was causing the escape codes. I changed it to just

RETURN "cursor.crstheapps"

and it worked as expected.

So, if I want to manually serialize a response and just return that character string, how would I tell WW not to serialize the response again?

Thanks! Kirk

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  Rick Strahl
  kirk allen
  Aug 17, 2021 @ 12:19pm

Why would you want to manually return JSON?

IAC, you can return a raw response by using the options on the JsonService instance. Please read through the Using wwRestProcess Class topic.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  kirk allen
  Rick Strahl
  Aug 19, 2021 @ 09:03am

Thanks for your help Rick.

And, about the manual return, I meant if I want to create my own JSON response and then just return that value, without that return value being automatically serialized. But now I don't think that will be an issue.

Thanks again, Kirk

Gravatar is a globally recognized avatar based on your email address. re: Escape characters in JSON responses
  Rick Strahl
  kirk allen
  Aug 19, 2021 @ 09:40am

Ok, but I don't see a scenario where that would make sense unless you have some sort of business component that creates JSON as output (which would be kinda weird since that's a output concern).

Anyway glad it's working now...

+++ Rick ---

© 1996-2021