Web Connection
Returning JSON from a WWWC app
Gravatar is a globally recognized avatar based on your email address. Returning JSON from a WWWC app
  Russell Campbell
  All
  Oct 21, 2025 @ 11:26am

I have some code that wants a JSON list like ["Choice1", "Choice2", "Choice3"] (not just three entries, but maybe 3 or 20 or 50 or so). How can I return that as JSON?

LOCAL loSerializer AS wwJsonSerializer
loSerializer = CREATEOBJECT("wwJsonSerializer")
RtnStr = loSerializer.Serialize(RtnStr)
Response.ContentType = "application/json"
Response.Write(RtnStr)

I used some code like the above where RtnStr = ["Choice1", "Choice2", "Choice3"] (with the brackets) and that did not work and I also tried it with the first three lines commented out and that did not work.

Gravatar is a globally recognized avatar based on your email address. re: Returning JSON from a WWWC app
  Rick Strahl
  Russell Campbell
  Oct 21, 2025 @ 02:13pm

If you have a JSON string that contains the array/collection data - just return it. Don't serialize it which turns it into a JSON string, which is not what you want here.

The two options are:

  • You're serializing a collection
    Use the JSON Serializer to create the string
  • Your returning an already created JSON value as a string
    Return the string directly - don't use the JSON Serializer
LOCAL loSerializer AS wwJsonSerializer
loSerializer = CREATEOBJECT("wwJsonSerializer")

*** Assuming RtnStr contains 
RtnStr = '["val1","val2","val3"]'   && You provide the JSON value as a string

*** Don't serialize the string
* RtnStr = loSerializer.Serialize(RtnStr)

*** Json Response
Response.ContentType = "application/json"
Response.Write(RtnStr)

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Returning JSON from a WWWC app
  Russell Campbell
  Rick Strahl
  Oct 21, 2025 @ 02:29pm

That's how I'd started out. Maybe there is a problem in the Javascript. I'll start from the top and go at it again. Thanks for your reply.

Gravatar is a globally recognized avatar based on your email address. re: Returning JSON from a WWWC app
  Rick Strahl
  Rick Strahl
  Oct 21, 2025 @ 04:59pm

You should never create JSON by hand because text needs to be potentially encoded.

For an array/collection use:

loCol = CREATEOBJECT("Collection")
loCol.Add("val1")
loCol.Add("val2")
loCol.Add("val3")

loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(loCol)

Response.ContentType = "application/json"
Response.Write(lcJson)
© 1996-2025