Web Connection User Discussions
Property name with space
Gravatar is a globally recognized avatar based on your email address. Property name with space
  Ganga
  All
  Dec 20, 2021 @ 11:16am

Hi Rick,

I would like to thank you for the amazing tool you have built. I found out about this tool few weeks ago via another forum. I watched your youtube, it was very educational and I was able to grasp the concepts. I am fairly new to the Webservices area though it has been around for many years (I just work on a vfp desktop application).

I have downloaded web connection practice version and following the tutorial.

How do I create property names with spaces? The reason for this is the JSON we need to send to an end point has property names with spaces.

Eg: loCust = CREATEOBJECT("Empty") ADDPROPERTY(loCust,"Client Name","Rick") ADDPROPERTY(loCust,"Client Company","West Wind Technologies")

VFP does not allow property names with spaces. How do we get around this? Please assist.

Thanks - Ganga

Gravatar is a globally recognized avatar based on your email address. re: Property name with space
  Rick Strahl
  Ganga
  Dec 20, 2021 @ 11:26am

I assume you're talking about creating JSON?

This is not directly supported - you can only do case conversions on the fly using PropertyNameOverrides on the JSON serializer.

But... you can fake it by doing something like this:

DO wwJsonSerializer

loSer = CREATEOBJECT("wwJsonSerializer")
loObj = CREATEOBJECT("EMPTY")
ADDPROPERTY(loObj,"LastName","Strahl")
ADDPROPERTY(loObj,"FirstName","Rick")

lcJson = loSer.Serialize(loObj)

*** { "lastname": "Strahl", "firstname": "Rick" }
? lcJson
?
lcJson = STRTRAN(lcJson,["lastname":],["Last Name":])
lcJson = STRTRAN(lcJson,["firstname":],["First Name":])

*** { "Last Name": "Strahl", "First Name": "Rick" }
? lcJson

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Property name with space
  Rick Strahl
  Ganga
  Dec 20, 2021 @ 11:43am

Here's a little useful helper method for wwJsonSerializer that I just added that makes this a little more transparent:

************************************************************************
*  MapPropertyName
****************************************
***  Function: Maps a property name in JSON to a new name
***            Useful if you need to map to a name that isn't valid 
***            as a FoxPro property like properties with spaces.*** 
***    Assume: lcJson is updated by reference (preferred) but value 
***            is also returned.
***      Pass: lcJson:  (pass by reference preferrably)
***            lcOrig:   original property name
***            lcNew:    new name
***    Return: (optional prefer using by ref) updated JSON
************************************************************************
FUNCTION MapPropertyName(lcJson, lcOriginalName, lcNewName)
lcJson = STRTRAN(lcJson,["] + lcOriginalName + [":], ["] + lcNewName + [":],-1,-1,1)
RETURN lcJson
ENDFUNC
*   MapPropertyName

With that you can now do:

loSer = CREATEOBJECT("wwJsonSerializer")
loObj = CREATEOBJECT("EMPTY")
ADDPROPERTY(loObj,"LastName","Strahl")
ADDPROPERTY(loObj,"FirstName","Rick")

lcJson = loSer.Serialize(loObj)
? lcJson
?
loSer.MapPropertyName(@lcJson, "lastName","Last Name")
loSer.MapPropertyName(@lcJson, "firstName","First Name")

? lcJson

Here is the documentation:

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Property name with space
  Ganga
  Rick Strahl
  Dec 20, 2021 @ 02:19pm

You are truly awesome Rick! Yes, that worked!

Thank you so much for the bonus helper method and the quick response 😃

© 1996-2024