Web Connection
Can't figure out how to create a Foxpro object containing an array within an array
Gravatar is a globally recognized avatar based on your email address. Can't figure out how to create a Foxpro object containing an array within an array
  Ron Jasper
  All
  Sep 4, 2025 @ 03:25pm

Hi Rick,

Here's the resultant JSON I need to create:

{
  "actions": [
    {
      "type": "add_custom_fields",
      "title": "We need some information about this return",
      "message": "Please explain why this item is being returned",
      "entity": "line_item",
      "entity_id": "cc0e2f8f-3c14-ac66-11ea-9e2e4fefb804",
      "skippable": true,
      "add_custom_fields": [
        {
          "name": "return-reason",
          "values": [
            {
              "value": "broken",
              "title": "The item is broken"
            },
            {
              "value": "wrong-size",
              "title": "The item does not fit"
            },
            {
              "value": "other",
              "title": "Other (Please describe below)"
            }
          ]
        },
        {
          "name": "return-note"
        }
      ]
    }
  ]
}

I can't figure out how to build the Foxpro object to pass to JSON serializer. Everytime I try to add the array add_custom_fields to the array actions I clobber the object.

Here's my Foxpro code:



RELEASE loThisResponse
PUBLIC loThisResponse AS OBJECT
LOCAL loThisResponse
loThisResponse = CREATEOBJECT("Empty")
loActions = CREATEOBJECT("Collection")
ADDPROPERTY(loThisResponse,"actions", loActions)
loCustomFields = CREATEOBJECT("Collection")
loCustom = CREATEOBJECT("EMPTY")
ADDPROPERTY(loCustom,"name","purchase_order")
loCustomFields.Add(loCustom)

ADDPROPERTY(loActions,"add_custom_fields", loCustomFields)

Any ideas?

Gravatar is a globally recognized avatar based on your email address. re: Can't figure out how to create a Foxpro object containing an array within an array
  Carl Chambers
  Ron Jasper
  Sep 4, 2025 @ 07:20pm

Hi Ron,
I think this does it...

LOCAL ARRAY laValues[3,2]
laValues[1,1] = "broken"
laValues[1,2] = "The item is broken"
laValues[2,1] = "wrong-size"
laValues[2,2] = "The item does not fit"
laValues[3,1] = "other"
laValues[3,2] = "Other (Please describe below)"

loValues = CREATEOBJECT("Collection")
FOR i = 1 TO 3
  loValue = CREATEOBJECT("empty")
  ADDPROPERTY(loValue,"value",laValues[i,1])
  ADDPROPERTY(loValue,"title",laValues[i,2])
  loValues.Add(loValue)
NEXT i

loCustomFlds = CREATEOBJECT("Collection")
loCustomFld = CREATEOBJECT("empty")
ADDPROPERTY(loCustomFld,"name","return-reason")
ADDPROPERTY(loCustomFld,"values",loValues)
loCustomFlds.Add(loCustomFld)
loCustomFld = CREATEOBJECT("empty")
ADDPROPERTY(loCustomFld,"name","return-note")
loCustomFlds.Add(loCustomFld)

loActions = CREATEOBJECT("Collection")
loAction = CREATEOBJECT("empty")
ADDPROPERTY(loAction,"type","add_custom_fields")
ADDPROPERTY(loAction,"title","We need some information about this return")
ADDPROPERTY(loAction,"message","Please explain why this item is being returned")
ADDPROPERTY(loAction,"entity","line_item")
ADDPROPERTY(loAction,"entity_id","cc0e2f8f-3c14-ac66-11ea-9e2e4fefb804")
ADDPROPERTY(loAction,"skippable",.T.)
ADDPROPERTY(loAction,"add_custom_fields",loCustomFlds)
loActions.Add(loAction)

loMaster = CREATEOBJECT("empty")
ADDPROPERTY(loMaster,"actions",loActions)

DO wwJsonSerializer.prg
loSer = CREATEOBJECT("wwJsonSerializer")
lcJSON = loSer.Serialize(loMaster,.T.)
MESSAGEBOX(lcJSON)

hth, Carl

Gravatar is a globally recognized avatar based on your email address. re: Can't figure out how to create a Foxpro object containing an array within an array
  Ron Jasper
  Carl Chambers
  Sep 5, 2025 @ 06:11am

Thanks Carl, I'll give that a shot, greatly appreciated

Gravatar is a globally recognized avatar based on your email address. re: Can't figure out how to create a Foxpro object containing an array within an array
  Rick Strahl
  Ron Jasper
  Sep 8, 2025 @ 11:10am

Another tip you can use to quickly create objects:

Deserialize from JSON. So for example in Carl's custom field definition you could replace that with:

TEXT TO lcCustomJson NOSHOW
 { 
      "type": "",
      "title": "",
      "message": "",
      "entity": "",
      "entity_id": "",
      "skippable": false,
      "add_custom_fields": []
}
ENDTEXT

loCustomFld = loSer.Deserialize(lcCustomJson)
loCustomFld.Type = "add_custom_fields"
loCustomFld.Title = "My Title"

loChild = loSer.Deserialize('{ value: "", title: "" }')
loChild.Value = "value 1"
loChild.Title = "title 1"
loCustomFld.Add_Custom_Fields.Add(loChild)

*** Also works - but not recommended unless you're sure values are simple ASCII
loChild = loSer.Deserialize('{ value: "value 2", title: "title 2" }')
loCustomFld.Add_Custom_Fld.Add(loChild)

Note that you shouldn't just TextMerge into the values, because in some scenarios you may have to JSON escape characters. It's better to create empty objects and assign values than to embed values into the JSON manually unless you know for sure the values are simple ASCII values.

You can do this for large or tiny objects where creating the code can be tedious. Note that code based object creation (ie. Carl's raw code) is going to be faster, but it probably only really matters if you're dealing with large objects that have lots and lots of child objects in collections that need to be created. For single or a few objects the deserialization is going to be very fast and efficient, but if you need max performance, manual object creation or better yet creating pre-coded FoxPro classes is going to be faster.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Can't figure out how to create a Foxpro object containing an array within an array
  Ron Jasper
  Rick Strahl
  Sep 8, 2025 @ 11:35am

Thanks for the tip, both are really helpful.

© 1996-2025