Fill element of json

Hi,

I need to create a json collection to submit to a webserver and has used Capesoft jFilesCode to create the structure.

Below you can se the json structure, the data declaration and the code I have that works.

What I am missing is filling one element of the subqueue “parcels”.
I have a brainfart and cannot get any code to work.

Any ideas?

tia
/Poul

json:
{{
  "product_code": "DAO_STS",
  "service_codes": "EMAIL_NT,SMS_NT",
  "sender": {{
    "name": "A-fotograf",
    "address1": "Tygestrupvej 17",
    "zipcode": "4180",
    "city": "Sorø",
    "country_code": "DK",
  },
  "parcels": [
    {{
      "weight": 249
    }
  ],
  "reference": "Ordre nr.: 126126"
}

data embed:
element             Group,Name('element'),PRE(elpre)
product_code            STRING(255),Name('product_code| opt')
service_codes           STRING(255),Name('service_codes| opt')
sender                  Group,Name('sender')
name                        STRING(255),Name('name')
address1                    STRING(255),Name('address1')
zipcode                     STRING(255),Name('zipcode')
city                        STRING(255),Name('city')
country_code                STRING(255),Name('country_code')
                        End
parcels                 &parcelsQueueType,Name('parcels| opt | queue')
reference               STRING(255),Name('reference| opt')
                    End

parcelsQueueType    Queue,Type,Name('parcels')
weight                  Real,Name('weight')
                    End

Json                JSONClass
str                   StringTheory

code:
    json.start()
    json.SetTagCase(jf:CaseAsIs)
    json.SetDontSaveBlanks(true)
    json.SetDontSaveBlankArrays(true)
    json.SetDontSaveBlankArrayValues(true)
    json.SetDontSaveBlankGroups(true)
    elpre:product_code          =  DAO_product_code
    elpre:service_codes         =  DAO_service_codes
    elpre:sender.name           =  DAO_afs_name
    elpre:sender.address1       =  DAO_afs_addr1
    elpre:sender.zipcode        =  DAO_afs_zipcode
    elpre:sender.city           =  DAO_afs_city
    elpre:sender.country_code   =  'DK'
    elpre:reference             =  CLIP('Ordre nr.: ' & clip(FOTo:ID) )    
     
!-->  missing parcels.weight

        json.Save(element,str)          ! Save to a StringTheory object
        json.DisposeGroup(element) !Optional Safe Tidy up of nested pointers  

If I understand what you are asking …

    json.start()
    json.SetTagCase(jf:CaseAsIs)
    json.SetDontSaveBlanks(true)
    json.SetDontSaveBlankArrays(true)
    json.SetDontSaveBlankArrayValues(true)
    json.SetDontSaveBlankGroups(true)
clear(elpre)  !Force of habit
    elpre:product_code          =  DAO_product_code
    elpre:service_codes         =  DAO_service_codes
    elpre:sender.name           =  DAO_afs_name
    elpre:sender.address1       =  DAO_afs_addr1
    elpre:sender.zipcode        =  DAO_afs_zipcode
    elpre:sender.city           =  DAO_afs_city
    elpre:sender.country_code   =  'DK'
    elpre:reference             =  CLIP('Ordre nr.: ' & clip(FOTo:ID) )    
     
!Add these lines, plus additional ADD calls on the parcles queue
element.parcels &= new(parcelsQueueType)
element.parcels.weight = 249
add(element.parcels)


        json.Save(element,str)          ! Save to a StringTheory object
        json.DisposeGroup(element) !Optional Safe Tidy up of nested pointers

Thanks Rick,

That is what I need, but the suggested code gives these errors:

Unknown variable type
Field not found: WEIGHT

I edited my answer and changed the elpre:parcels usage to element.parcels.
I never use prefix:queuefield syntax, but wanted to follow the format you started with.
I use dot notation. So instead of elpre:field, I’d use element.field

Thanks Rick - that did it :slight_smile:

The reason I am using prefix:queuefield is caused by the fact that I could not get Code Completion to work without it, so I added the prefix.
Now CC works without it, go figure…

1 Like